Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. It is found in the java.util package. It is like the Vector in C++.
The ArrayList in Java can have the duplicate elements also. It implements the List interface so we can use all the methods of List interface here. The ArrayList maintains the insertion order internally.
It inherits the AbstractList class and implements List interface.
The important points about Java ArrayList class are:
Hierarchy of ArrayList class
As shown in the above diagram, Java ArrayList class extends AbstractList class which implements List interface. The List interface extends the Collection and Iterable interfaces in hierarchical order.
ArrayList class declaration
Let's see the declaration for java.util.ArrayList class.
Constructors of ArrayList
Methods of ArrayList
Java Non-generic Vs. Generic Collection
Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic.
Java new generic collection allows you to have only one type of object in a collection. Now it is type safe so typecasting is not required at runtime.
Let's see the old non-generic example of creating java collection.
Let's see the new generic example of creating java collection.
In a generic collection, we specify the type in angular braces. Now ArrayList is forced to have the only specified type of objects in it. If you try to add another type of object, it gives compile time error.
For more information on Java generics, click here Java Generics Tutorial.
Java ArrayList ExampleTest it Now
[Mango, Apple, Banana, Grapes]
Iterating ArrayList using Iterator
Let's see an example to traverse ArrayList elements using the Iterator interface.Test it Now
Mango Apple Banana Grapes
Iterating ArrayList using For-each loop
Let's see an example to traverse the ArrayList elements using the for-each loop
Output:Test it Now
Mango Apple Banana Grapes
Get and Set ArrayList
The get() method returns the element at the specified index, whereas the set() method changes the element.Test it Now
Returning element: Apple Mango Dates Banana Grapes
How to Sort ArrayList
The java.util package provides a utility class Collections which has the static method sort(). Using the Collections.sort() method, we can easily sort the ArrayList.
Apple Banana Grapes Mango Sorting numbers... 1 11 21 51
Ways to iterate the elements of the collection in Java
There are various ways to traverse the collection elements:
Iterating Collection through remaining ways
Let's see an example to traverse the ArrayList elements through other ways
Traversing list through List Iterator: Ajay Ravi Vijay Ravi Traversing list through for loop: Ravi Vijay Ravi Ajay Traversing list through forEach() method: Ravi Vijay Ravi Ajay Traversing list through forEachRemaining() method: Ravi Vijay Ravi Ajay
User-defined class objects in Java ArrayList
Let's see an example where we are storing Student class object in an array list.
101 Sonoo 23 102 Ravi 21 103 Hanumat 25
Java ArrayList Serialization and Deserialization Example
Let's see an example to serialize an ArrayList object and then deserialize it.
[Ravi, Vijay, Ajay]
Java ArrayList example to add elements
Here, we see different ways to add an element.
Initial list of elements:  After invoking add(E e) method: [Ravi, Vijay, Ajay] After invoking add(int index, E element) method: [Ravi, Gaurav, Vijay, Ajay] After invoking addAll(Collection<? extends E> c) method: [Ravi, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addAll(int index, Collection<? extends E> c) method: [Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat]
Java ArrayList example to remove elements
Here, we see different ways to remove an element.
An initial list of elements: [Ravi, Vijay, Ajay, Anuj, Gaurav] After invoking remove(object) method: [Ravi, Ajay, Anuj, Gaurav] After invoking remove(index) method: [Ajay, Anuj, Gaurav] Updated list : [Ajay, Anuj, Gaurav, Ravi, Hanumat] After invoking removeAll() method: [Ajay, Anuj, Gaurav] After invoking removeIf() method: [Anuj, Gaurav] After invoking clear() method: 
Java ArrayList example of retainAll() method
iterating the elements after retaining the elements of al2 Ravi
Java ArrayList example of isEmpty() method
Is ArrayList Empty: true After Insertion Is ArrayList Empty: false
Java ArrayList Example: Book
Let's see an ArrayList example where we are adding books to list and printing all the books.Test it Now
101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications and Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6