ArrayList in Java
Last Updated : 3 Feb, 2026
ArrayList in Java is a resizable array provided in the [Link] package. Unlike
normal arrays, its size can grow or shrink dynamically as elements are added or
removed.
Elements can be accessed using their index, just like arrays.
Duplicate elements are allowed.
Elements are stored in the order they are inserted.
ArrayList is not thread-safe. To make it thread-safe, you must wrap it manually
using [Link]() .
import [Link];
class Main {
public static void main (String[] args) {
// Creating an ArrayList
ArrayList<Integer> a = new ArrayList<Integer>();
// Adding Element in ArrayList
[Link](1);
[Link](2);
[Link](3);
// Printing ArrayList
[Link](a);
}
}
Output
[1, 2, 3]
Explanation: This program creates an ArrayList of integers, adds elements to it using
the add() method, and stores them dynamically. Finally, it prints the elements in
insertion order as [1, 2, 3].
Hierarchy of ArrayList
It implements List Interface which is a sub-interface of Collection Interface.
Java - ArrayList 1/6
ArrayList Constructors in Java
Java provides multiple constructors to create an ArrayList based on different
requirements:
1. ArrayList()
Creates an empty ArrayList with default initial capacity.
ArrayList<Integer> arr = new ArrayList<>();
2. ArrayList(Collection<? extends E> c)
Creates an ArrayList initialized with elements from the specified collection.
ArrayList<String> arr = new ArrayList<>(collection);
3. ArrayList(int initialCapacity)
This constructor is used to build an array list with the initial capacity being specified.
ArrayList<Double> arr = new ArrayList<>(20);
Operations of ArrayList
Now, Using the constructors we have got ArrayList for further operations like
Insertion,Deletion and Updation of the elements in ArrayList.
import [Link].*;
class GFG{
public static void main(String args[]){
// Creating an Array of string type
ArrayList<String> al = new ArrayList<>();
// 1. Adding elements to ArrayList at the end
[Link]("Geeks");
Java - ArrayList 2/6
[Link]("Geeks");
[Link]("Original List : "+al);
// Adding Elements at the specific index
[Link](1, "For");
[Link]("After Adding element at index 1 : "+ al);
// 2. Removing Element using index
[Link](0);
[Link]("Element removed from index 0 : "+ al);
// Removing Element using the value
[Link]("Geeks");
[Link]("Element Geeks removed : "+ al);
// 3. Updating value at index 0
[Link](0, "GFG");
[Link]("List after updation of value : "+al);
}
}
Try it on GfG Practice
Output
Original List : [Geeks, Geeks]
After Adding element at index 1 : [Geeks, For, Geeks]
Element removed from index 0 : [For, Geeks]
Element Geeks removed : [For]
List after updation of value : [GFG]
Complexity of Java ArrayList
Operation Time Complexity Space Complexity
Inserting Element in
O(1) O(N)
ArrayList
Removing Element from
O(N) O(1)
ArrayList
Java - ArrayList 3/6
Operation Time Complexity Space Complexity
Traversing Elements in
O(N) O(N)
ArrayList
Replacing Elements in
O(1) O(1)
ArrayList
Java ArrayList Methods
Method Description
add(int index, Object This method is used to insert a specific
element) element at a specific position index in a list.
This method is used to append a specific
add(Object o)
element to the end of a list.
This method is used to append all the
elements from a specific collection to the end
addAll(Collection C) of the mentioned list, in such an order that
the values are returned by the specified
collection’s iterator.
Used to insert all of the elements starting at
addAll(int index,
the specified position from a specific
Collection C)
collection into the mentioned list.
This method is used to remove all the
clear()
elements from any list.
This method is used to return a shallow copy
clone()
of an ArrayList in Java.
Returns true if this list contains the specified
contains(Object o)
element.
Increases the capacity of this ArrayList
ensureCapacity(int instance, if necessary, to ensure that it can
minCapacity) hold at least the number of elements specified
by the minimum capacity argument.
Java - ArrayList 4/6
Method Description
Performs the given action for each element of
forEach(Consumer<?
the Iterable until all elements have been
super E> action)
processed or the action throws an exception.
Returns the element at the specified position
get(int index)
in this list.
The index the first occurrence of a specific
indexOf(Object O) element is either returned or -1 in case the
element is not in the list.
isEmpty() Returns true if this list contains no elements.
The index of the last occurrence of a specific
lastIndexOf(Object O) element is either returned or -1 in case the
element is not in the list.
Returns a list iterator over the elements in
listIterator()
this list (in proper sequence).
Returns a list iterator over the elements in
listIterator(int index) this list (in proper sequence), starting at the
specified position in the list.
Removes the element at the specified
remove(int index)
position in this list.
Removes the first occurrence of the specified
remove(Object o)
element from this list, if it is present.
Removes from this list all of its elements that
removeAll(Collection c)
are contained in the specified collection.
Removes all of the elements of this collection
removeIf(Predicate filter)
that satisfy the given predicate.
removeRange(int Removes from this list all of the elements
fromIndex, int toIndex) whose index is between from Index, inclusive
Java - ArrayList 5/6
Method Description
and to Index, exclusive.
Retains only the elements in this list that are
retainAll(Collection<?> c)
contained in the specified collection.
Replaces the element at the specified position
set(int index, E element)
in this list with the specified element.
size() Returns the number of elements in this list.
Creates a late-binding and fail-fast Spliterator
spliterator()
over the elements in this list.
Returns a view of the portion of this list
subList(int fromIndex, int
between the specified fromIndex, inclusive
toIndex)
and toIndex, exclusive.
This method is used to return an array
toArray() containing all of the elements in the list in the
correct order.
It is also used to return an array containing all
toArray(Object[] O) of the elements in this list in the correct order
same as the previous method.
This method is used to trim the capacity of
trimToSize() the instance of the ArrayList to the list's
current size.
Java - ArrayList 6/6