Java ArrayList is used to store and manage a collection of data that can grow or shrink dynamically.
In Java, traditional arrays have a fixed size, which makes them less flexible. To solve this problem, ArrayList provides a dynamic way to store elements that allow you to add or remove items easily without worrying about size.
ArrayList is a resizable (dynamic) array that is part of the Java Collections Framework. It allows you to store elements dynamically without worrying about the initial size.
Unlike standard arrays, an ArrayList automatically adjusts its capacity as elements are added or removed.
The ArrayList in Java can also have duplicate elements. It implements the List interface so that we can use all the methods of the List interface here. The ArrayList maintains the insertion order internally.
It inherits the AbstractList class and implements List interface.
The ArrayList provides several useful features that make it flexible and easy to use for storing and managing data dynamically.
The Java ArrayList class is part of the Java Collections Framework and follows a hierarchical structure.
This hierarchy allows ArrayList to inherit useful methods and support features like iteration, dynamic storage, and collection operations.
The below image shows the hierarchy of the ArrayList class:

Let's see the declaration of the java.util.ArrayList class:
Explanation:
ArrayList provides multiple constructors that allow you to create a list with a default size, a specified initial capacity, or by copying elements from another collection.
| Constructor | Description |
|---|---|
| ArrayList() | It is used to build an empty array list. |
| ArrayList(Collection<? extends E> c) | It is used to build an array list that is initialized with the elements of the collection c. |
| ArrayList(int capacity) | It is used to build an array list that has the specified initial capacity. |
ArrayList provides various methods to add, remove, access, and manipulate elements within the list.
| Method | Description |
|---|---|
| void add(int index, E element) | It is used to insert the specified element at the specified position in a list. |
| boolean add(E e) | It is used to append the specified element at the end of a list. |
| boolean addAll(Collection<? extends E> c) | It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. |
| boolean addAll(int index, Collection<? extends E> c) | It is used to append all the elements in the specified collection, starting at the specified position of the list. |
| void clear() | It is used to remove all of the elements from this list. |
| void ensureCapacity(int requiredCapacity) | It is used to enhance the capacity of an ArrayList instance. |
| E get(int index) | It is used to fetch the element from the particular position of the list. |
| boolean isEmpty() | It returns true if the list is empty, otherwise false. |
| Iterator() | Returns an iterator over the elements in the ArrayList in proper sequence. Allows sequential access to the elements in the ArrayList. |
| listIterator() | Returns a list iterator over the elements in the ArrayList in proper sequence. Allows bidirectional access to the elements in the ArrayList, including adding, removing, and modifying elements during iteration. |
| int lastIndexOf(Object o) | It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element. |
| Object[] toArray() | It is used to return an array containing all of the elements in this list in the correct order. |
| <T> T[] toArray(T[] a) | It is used to return an array containing all of the elements in this list in the correct order. |
| Object clone() | It is used to return a shallow copy of an ArrayList. |
| boolean contains(Object o) | It returns true if the list contains the specified element. |
| int indexOf(Object o) | It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. |
| E remove(int index) | It is used to remove the element present at the specified position in the list. |
| boolean remove(Object o) | It is used to remove the first occurrence of the specified element. |
| boolean removeAll(Collection<?> c) | It is used to remove all the elements from the list. |
| boolean removeIf(Predicate<? super E> filter) | It is used to remove all the elements from the list that satisfies the given predicate. |
| protected void removeRange(int fromIndex, int toIndex) | It is used to remove all the elements lies within the given range. |
| void replaceAll(UnaryOperator<E> operator) | It is used to replace all the elements from the list with the specified element. |
| void retainAll(Collection<?> c) | It is used to retain all the elements in the list that are present in the specified collection. |
| E set(int index, E element) | It is used to replace the specified element in the list, present at the specified position. |
| void sort(Comparator<? super E> c) | It is used to sort the elements of the list on the basis of the specified comparator. |
| Spliterator<E> spliterator() | It is used to create a spliterator over the elements in a list. |
| List<E> subList(int fromIndex, int toIndex) | It is used to fetch all the elements that lies within the given range. |
| int size() | It is used to return the number of elements present in the list. |
| void trimToSize() | It is used to trim the capacity of this ArrayList instance to be the list's current size. |
You can traverse an ArrayList using an Iterator. It provides a safe way to loop through elements, especially when modifying the list during iteration.
This example demonstrates how to iterate through an ArrayList using the Iterator interface in Java.
Output:
Mango Apple Banana Grapes
The for-each loop (enhanced for loop) provides a simple and readable way to iterate through elements of an ArrayList.
This example shows how to traverse an ArrayList using the for-each loop in a simple and readable way.
Output:
Mango Apple Banana Grapes
You can access and modify elements in an ArrayList using the get() and set() methods.
This example explains how to access and modify elements in an ArrayList using the get() and set() methods.
Output:
Returning element: Apple Mango Dates Banana Grapes
You can sort an ArrayList using the Collections.sort() method from the java.util package.
This example demonstrates how to sort elements of an ArrayList using the Collections.sort() method.
Output:
Apple Banana Grapes Mango Sorting numbers... 1 11 21 51
You can traverse an ArrayList using multiple methods like ListIterator, for loop, forEach(), and forEachRemaining().
This example demonstrates different ways to iterate through an ArrayList.
Output:
ListIterator (reverse): Ajay Ravi Vijay Ravi For loop: Ravi Vijay Ravi Ajay forEach(): Ravi Vijay Ravi Ajay forEachRemaining(): Ravi Vijay Ravi Ajay
ArrayList can store objects of user-defined classes, making it useful for managing custom data.
This example demonstrates how to store and access user-defined class objects in an ArrayList.
Output:
101 Sonoo 23 102 Ravi 21 103 Hanumat 25
Serialization converts an object into a byte stream, while deserialization restores it back.
This example demonstrates how to serialize and deserialize an ArrayList.
Output:
[Ravi, Vijay, Ajay]
You can add elements using methods like add() and addAll().
This example demonstrates different ways to add elements to an ArrayList.
Output:
[Ravi, Gaurav, Vijay, Ajay, Sonoo, Hanumat]
ArrayList provides multiple methods to remove elements.
This example demonstrates different ways to remove elements from an ArrayList.
Output:
[Ajay]
The retainAll() method keeps only common elements between two collections.
This example demonstrates how to retain common elements in an ArrayList.
Output:
[Ravi]
The isEmpty() method checks whether the ArrayList is empty.
This example demonstrates how to check if an ArrayList is empty.
Output:
true false
ArrayList can store complex objects like books.
This example demonstrates how to store and display objects in an ArrayList.
Output:
101 Java 102 Python
Size is the number of elements in the list, while capacity is the internal storage size.
This example demonstrates the difference between size and capacity of an ArrayList.
Output:
Size: 0
Explanation: The output makes sense as we have not done anything with the array list. Now observe the following program.
We request you to subscribe our newsletter for upcoming updates.