Java 向数组中添加对象
本教程介绍如何在 Java 中将对象添加到自定义类的数组中。
Java 是一种面向对象的编程语言,一切都围绕着对象展开。所有数据结构容器(List、Array、Set、set)都以对象形式存储/保存数据。我们也可以创建一个自定义类的数组,并将对象存储到其中。
在本文中,我们首先创建一个自定义类的数组,然后将这个类的对象存储到 this 中。所以,让我们从一些例子开始。
在 Java 中将对象添加到自定义类的数组
在这个例子中,我们创建了一个包含三个字段的 Student
类,在 SimpleTesting
类中,我们创建了一个这个类的数组,稍后我们将一个对象存储到这个数组中。请参阅下面的示例。
public class SimpleTesting{
public static void main(String[] args){
Student[] studentArray = new Student[5];
Student student = new Student(1, "Rohan", 40);
studentArray[0] = student;
System.out.println(studentArray[0].getId());
System.out.println(studentArray[0].getName());
System.out.println(studentArray[0].getAge());
}
}
class Student{
int id;
String name;
int age;
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
输出:
1
Rohan
40
在 Java 中将对象添加到自定义类的列表中
这是存储自定义类对象的另一种解决方案。在这里,我们使用列表而不是数组来添加对象。与数组相比,列表有几个优点,例如内置方法支持和动态调整大小。
在这里,我们首先创建了一个仅接受学生类对象的列表,然后我们创建了一个学生类对象并使用 add()
方法将其添加到列表中。
访问列表元素。我们使用了 get()
方法。请参阅下面的示例。
import java.util.ArrayList;
import java.util.List;
public class SimpleTesting{
public static void main(String[] args){
List<Student> studentList = new ArrayList<>();
Student student = new Student(1, "Rohan", 40);
studentList.add(student);
System.out.println(studentList.get(0).getId());
System.out.println(studentList.get(0).getName());
System.out.println(studentList.get(0).getAge());
}
}
class Student{
int id;
String name;
int age;
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
输出:
1
Rohan
40
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
将 NumPy 数组转换为 Pandas DataFrame
发布时间:2024/04/21 浏览次数:111 分类:Python
-
本教程介绍了如何使用 pandas.DataFrame()方法从 NumPy 数组生成 Pandas DataFrame。
如何在 Pandas 中使用默认值向现有 DataFrame 添加新列
发布时间:2024/04/20 浏览次数:81 分类:Python
-
我们可以使用分配或插入方法并通过设置新列系列的值,在 Pandas 中使用默认值向现有 DataFrame 添加新列。
如何在 Pandas DataFrame 中添加一行
发布时间:2024/04/20 浏览次数:223 分类:Python
-
本教程演示如何向 Pandas DataFrame 添一行,例如 loc,字典,追加等。
如何将 Pandas Dataframe 转换为 NumPy 数组
发布时间:2024/04/20 浏览次数:176 分类:Python
-
本教程介绍如何将 Pandas Dataframe 转换为 NumPy 数组的方法,例如 to_numpy,value 和 to_records
循环 PHP MySQLi 获取数组函数
发布时间:2024/03/25 浏览次数:125 分类:MySQL
-
本教程将指导你了解 php mysqli_fetch_array() 函数,并介绍如何迭代 mysqli 查询。
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。