
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How Objects Can an ArrayList Hold in Java
ArrayList is a class of Java Collection Framework that implements List Interface. It is a linear structure that stores and accesses each object in a sequential manner. It allows the storage of duplicate values. Always remember, each class of the collection framework can hold instances of wrapper classes or custom objects. They do not work with primitives. This article aims to explain how those objects hold by an ArrayList in Java.
How ArrayList holds Objects
ArrayList internally uses an array to store its elements. However, the size of the array is not fixed, it can increase and decrease as per the needs. When an ArrayList is created, it has an initial capacity, which is the number of elements that the array can hold without resizing. The default initial capacity is 10, but we can specify it in the constructor.
When an element is added to an ArrayList, it checks whether the array has enough space to store the new element or not. If yes, it simply assigns the element to the next available slot in the array. If no, it creates a new array with double capacity of the current size, copies all the existing elements from the old array to the new array, and then appends the new element to the newly created array. This process is called as resizing or rehashing.
When an element is removed from an ArrayList, it shifts all the elements after that position to the left by one index. This creates a gap at the end of the array, which reduces the effective size of the ArrayList, but the capacity remains unchanged until a new element is added that requires resizing.
The advantage of using ArrayList is that it provides fast and random access to any element by its index since it internally uses an array. The disadvantage is that adding and removing elements can be slow, especially when resizing is involved since it requires creating a new array and copying all the elements.
Syntax
ArrayListcollection_name = new ArrayList<>();
Approach
The first step is to import the ?java.util' package to enable the use of ArrayList
Then, define an ArrayList of type ?Integer' and store a few elements in it using ?add()' method.
In the main() method, take a for-each loop and iterate till the size of ArrayList to print its objects on the screen.
Example 1
Let's see how we can create an ArrayList to hold objects.
import java.util.*; public class ArrayObj { public static void main(String[] args) { // Creating arraylist ArrayList<Integer> araylist = new ArrayList<>(); // Adding elements in arraylist araylist.add(1); araylist.add(2); araylist.add(1); araylist.add(0); araylist.add(9); araylist.add(6); System.out.println("Elements of the list : "); // loop to iterate through elements for(int i = 0; i < araylist.size(); i++ ) { // to print the elements in the list System.out.println(araylist.get(i)); } } }
Output
Elements of the list : 1 2 1 0 9 6
Approach
Create a class ?Cart' and inside it, declare two variables and also define a constructor of this class along with two parameters of type string and integer
Moving further we will convert the data of object into a string using ?toString()' method.
Now, in the main() method, declare a collection named ?obj' of ArrayList of type ?Cart' and using the in-built method named ?add()' store the details of object to the collection.
Take a for-each loop and iterate till the size of ArrayList to print its objects on the screen
Example 2
In the following example, we will create an ArrayList to hold custom objects
import java.util.*; public class Cart { String item; int price; Cart(String item, int price) { // this keyword shows these variables belong to constructor this.item = item; this.price = price; } // method for converting object into string public String toString() { return "Item: " + item + ", " + "Price: " + price; } public static void main(String[] args) { // Declaring collection arraylist ArrayList<Cart> obj = new ArrayList<>(); // Adding object to the collection obj.add(new Cart("Rice", 59)); obj.add(new Cart("Milk", 60)); obj.add(new Cart("Bread", 45)); obj.add(new Cart("Peanut", 230)); obj.add(new Cart("Butter", 55)); // to print list of objects System.out.println("The Objects in the List: "); for(Cart print : obj) { System.out.println(print); } } }
Output
The Objects in the List: Item: Rice, Price: 59 Item: Milk, Price: 60 Item: Bread, Price: 45 Item: Peanut, Price: 230 Item: Butter, Price: 55
Conclusion
We started this article by defining the ArrayList and in the later section, we explained how it holds objects in detail. Also, we discussed two example programs for our better understanding of object storage in ArrayList.