An ArrayList in Java represent a resizable list of objects. We can add, remove, find, sort and replace elements in this list. ArrayList is part of Java’s collection framework and implements Java’s List interface.
Hierarchy of ArrayList class
Java ArrayList class extends AbstractList
class which implements List
interface. The List interface extends Collection
and Iterable
interfaces in hierarchical order.

1. ArrayList Features
ArrayList has following features –
- Ordered – Elements in arraylist preserve their ordering which is by default the order in which they were added to the list.
- Index based – Elements can be randomly accessed using index positions. Index start with
'0'
. - Dynamic resizing – ArrayList grows dynamically when more elements needs to be added than it’s current size.
- Non synchronized – ArrayList is not synchronized, by default. Programmer needs to use
synchronized
keyword appropiately or simply use Vector class. - Duplicates allowed – We can add duplicate elements in arraylist. It is not possible in sets.
2. Internal Working of ArrayList
ArrayList class is implemented arounda backing array. The elements added or removed from arraylist are actually modified in this backing array. All arraylist methods access this array and get/set elements in the array.
ArrayList basically cab be seen as resizable-array implementation in Java.
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { transient Object[] elementData; //backing array private int size; //array or list size //more code }
3. ArrayList Examples
3.1. Create ArrayList
Usually we will create either an empty list and add elements to it. Or we will create an arraylist with another exising collection.
//Empty arraylist List<String> names = new ArrayList<>(); //Arraylist initialized with another collection List<Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5));
3.2. Add and remove elements
Use add()
, set()
and remove()
methods to add or update the elements in the list.
//Create arraylist List<String> names = new ArrayList<>(); names.add("lokesh"); //[lokesh] names.add("alex"); //[lokesh, alex] names.set(1, "brian"); //[lokesh, brian] names.remove(1); //[lokesh]
3.2. Iterate
Use iterator()
or listIterator()
to get the reference of iterator instance. This iterator can be used to iterate the elements in the arraylist.
ArrayList<Integer> digits = new ArrayList<>(Arrays.asList(1,2,3,4,5,6)); Iterator<Integer> iterator = digits.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); }
Program output.
1 2 3 4 5 6
4. ArrayList Methods
ArrayList add() method example
ArrayList addAll() method example
ArrayList clear() method example
ArrayList clone() – How to clone an ArrayList
ArrayList contains() method example
ArrayList ensureCapacity() method example
ArrayList forEach() method example
ArrayList get() method example
ArrayList indexOf() method example
ArrayList lastIndexOf() method example
ArrayList listIterator() method example
ArrayList remove() method example
ArrayList removeAll() method example
ArrayList retainAll() method example
ArrayList replaceAll() method example
ArrayList removeIf() method example
ArrayList sort() method example
ArrayList spliterator() method example
ArrayList subList() method example
ArrayList toArray() method example
5. Java ArrayList Examples
5.1. Create arraylist
Initialize ArrayList
Iterate through ArrayList
5.2. Add elements and remove elements
Add element at particular index of ArrayList
Remove element from ArrayList
Add multiple items to ArrayList
5.3. Sort arraylist
Sort ArrayList
Sort ArrayList of Objects using Comparable and Comparator
Sort ArrayList of objects by multiple fields
Sort ArrayList of objects using Collections.sort() method
5.4. Get/Search
Get Sub List of ArrayList
Find the index of last index of the element in the ArrayList
Get the index of the element in the ArrayList
Get element from ArrayList
Check if element exists in ArrayList
6. Other Tutorials on Java ArrayList
Compare two ArrayLists
Synchronize ArrayList
Swap two elements in ArrayList
Serialize ArrayList
Join two ArrayList
Make ArrayList Empty
Check whether ArrayList is empty or not
Replace the value of existing element in ArrayList
Remove duplicate elements in ArrayList
7. Conversions
Convert LinkedList to ArrayList
Convert Vector to ArrayList
Convert ArrayList to String Array
Convert Array to ArrayList
Convert HashSet to ArrayList
8. Differences
ArrayList vs Vector
ArrayList vs LinkedList
References: