Add Objects to an Array in Java

This tutorial introduces how to add objects to an array of a custom class in Java.
Java is an object-oriented programming language, and everything revolves around the object. All the data structure containers (List, Array, Set, set) store/hold data in object form. We can create an array of a custom class as well and can store objects to it.
In this article, we first create an array of a custom class and then store objects of this class into this. So, let’s start with some examples.
Add Objects to an Array of a Custom Class in Java
In this example, we created a Student
class with three fields, and inside the SimpleTesting
class, we created an array of this class, later we stored an object to this array. See the example below.
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;
}
}
Output:
1
Rohan
40
Add Objects to a List of a Custom Class in Java
This is another solution to store objects of custom class. Here, we used a list instead of an array to add objects. The list has several advantages over the array, such as built-in method supports and dynamic sizing.
Here, we first created a list that takes objects of student class only, and then we created an object of student class and added it to the list using the add()
method.
To access list elements. we used the get()
method. See the example below.
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;
}
}
Output:
1
Rohan
40
Related Article - Java Array
- Concatenate Two Arrays in Java
- Convert Byte Array in Hex String in Java
- Remove Duplicates From Array in Java
- Count Repeated Elements in an Array in Java
- Natural Ordering in Java
- Slice an Array in Java