How to Add an Object in an Arraylist in Java
In Java, ArrayList
is a resizable array and can also be defined as an ordered sequence of elements. Unlike simple arrays, the Java ArrayList
is more flexible and can hold multiple data types. This article will demonstrate how you can utilize this function.
Add Objects of the Same Type in an ArrayList
Here, we will add user-defined or custom class objects to an ArrayList
. In ArrayList
, we can access the elements using the integer index. We’ll specify or declare the type of object we will store in the ArrayList
inside the <>
(angle brackets).
In the code below, we have a Book
class object with a constructor and three instance variables bookName
, author
, and rating
of data type, respectively. We create a Book
class object using the new
keyword followed by the constructor call with the parameters; this assigns the passed value to the instance variables.
The add()
method inserts objects into the arrayofBooks
. Thus, we added 4 Book
class objects to our arrayOfBooks
. We then run a foreach
loop to iterate and display all the Book
data.
We can access the attributes of the Book
class by using the .
dot operator. Below we accessed the bookName
attribute by calling the book.bookName
function.
import java.util.ArrayList;
public class ArrayObject {
public static void main(String args[]) {
ArrayList<Book> arrayOfBooks = new ArrayList<>();
arrayOfBooks.add(new Book("To Kill a Mockingbird", "Harper Lee", 3));
arrayOfBooks.add(new Book("1984", "George Orwell", 4));
arrayOfBooks.add(new Book("Harry Potter and the Philosopher's Stone", "J.K. Rowling", 4));
arrayOfBooks.add(new Book("The Lord of the Rings", "J.R.R. Tolkien", 4.5));
for (Book book : arrayOfBooks) {
System.out.println(
"BookTitle: " + book.bookName + ", by" + book.author + "with a rating of " + book.rating);
}
}
}
class Book {
String bookName;
String author;
double rating;
Book(String bookName, String author, double rating) {
this.bookName = bookName;
this.author = author;
this.rating = rating;
}
}
Output:
BookTitle: To Kill a Mockingbird, byHarper Leewith a rating of 3.0
BookTitle: 1984, byGeorge Orwellwith a rating of 4.0
BookTitle: Harry Potter and the Philosopher's Stone, byJ.K. Rowlingwith a rating of 4.0
BookTitle: The Lord of the Rings, byJ.R.R. Tolkienwith a rating of 4.5
Add Objects of Different Types in an ArrayList
As mentioned, the function ArrayList
can also hold multiple types of objects. Here, arrayOfDifferentObject
is an ArrayList
that can hold objects of different types. We declared our ArrayList
using the <Object>
class in the syntax given below in code.
In Java, ArrayList
can hold objects of wrapper classes like double, integer, and string.
We then add elements to the ArrayList
using the add()
method. Firstly, we added a string value to our ArrayList
, then a double value, integer, and float, respectively. We can also replace an element with a new value at the index of our choice using the set()
method.
We replaced the arrayOfDifferentObject.set(1,"David Wells")
and the double value at index 1 with a string value. After that, we can see that the output is modified.
import java.util.ArrayList;
public class ArrayObject {
public static void main(String args[]) {
ArrayList<Object> arrayOfDifferentObject = new ArrayList<Object>();
arrayOfDifferentObject.add("John Doe");
arrayOfDifferentObject.add(10.00D);
arrayOfDifferentObject.add(10);
arrayOfDifferentObject.add(10.11F);
System.out.println("ArrayList after all insertion:-");
for (int i = 0; i < arrayOfDifferentObject.size(); i++) {
System.out.println(arrayOfDifferentObject.get(i));
}
arrayOfDifferentObject.set(1, "David Wells");
System.out.println("ArrayList after modification:-");
for (int i = 0; i < arrayOfDifferentObject.size(); i++) {
System.out.println(arrayOfDifferentObject.get(i));
}
}
}
Output:
ArrayList after all insertion:-
John Doe
10.0
10
10.11
ArrayList after modification:-
John Doe
David Wells
10
10.11
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn