Java ArrayList Class Methods

Last Updated : 8 Sept 2026

Java ArrayList class is a member of Java Collections Framework that belongs to java.util package. It implements the List interface so that we can use all the methods of the List interface.  It inherits the AbstractList class.

Like normal arrays, its size can grow or shrink dynamically as elements are added or removed. It allows us to store duplicate and null elements. The ArrayList maintains the insertion order internally. The implementation of ArrayList is not thread-safe.

The ArrayList Class maintains two values:

  • Size: It is the number of elements currently stored.
  • Capacity: It is the total length of the internal array that can hold elements before resizing is required.

ArrayList Class Constructors

ArrayList provides multiple constructors that allow us to create a list of default size, a specified initial capacity, or by copying elements from another collection.

ConstructorDescription
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 Class Methods

ArrayList class provides various methods to add, remove, access, and manipulate elements within the list.

MethodDescription
add(int index, E element)It is used to insert the specified element at the specified position in a list.
add(E e)It is used to append the specified element at the end of a list.
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.
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.
 clear()It is used to remove all of the elements from this list.
ensureCapacity(int requiredCapacity)It is used to enhance the capacity of an ArrayList instance.
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()It returns an iterator over the elements in the ArrayList in proper sequence.
Allows sequential access to the elements in the ArrayList.
listIterator()It 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.
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.
toArray()It is used to return an array containing all of the elements in this list in the correct order.
toArray(T[] a)It is used to return an array containing all of the elements in this list in the correct order.
clone()It is used to return a shallow copy of an ArrayList.
contains(Object o)It returns true if the list contains the specified element.
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.
remove(int index)It is used to remove the element present at the specified position in the list.
remove(Object o)It is used to remove the first occurrence of the specified element.
removeAll(Collection<?> c)It is used to remove all the elements from the list.
removeIf(Predicate<? super E> filter)It is used to remove all the elements from the list that satisfies the given predicate.
removeRange(int fromIndex, int toIndex)It is used to remove all the elements lies within the given range.
replaceAll(UnaryOperator<E> operator)It is used to replace all the elements from the list with the specified element.
retainAll(Collection<?> c)It is used to retain all the elements in the list that are present in the specified collection.
set(int index, E element)It is used to replace the specified element in the list, present at the specified position.
sort(Comparator<? super E> c)It is used to sort the elements of the list on the basis of the specified comparator.
spliterator()It is used to create a spliterator over the elements in a list.
subList(int fromIndex, int toIndex)It is used to fetch all the elements that lies within the given range.
size()It is used to return the number of elements present in the list.
trimToSize()It is used to trim the capacity of this ArrayList instance to be the list's current size.

Example: Performing Basic Operations in an ArrayList

Java

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<String> names = new ArrayList<>();
        //add()
        names.add("Tom");
        names.add("Robert");
        names.add("Andrew");
        System.out.println("After add(): " + names);
        //add(index, element)
        names.add(1, "Jack");
        System.out.println("After add(index, element): " + names);
        //get()
        System.out.println("Element at index 2: " + names.get(2));
        //set()
        names.set(2, "Mary");
        System.out.println("After set(): " + names);
        //remove(index)
        names.remove(1);
        System.out.println("After remove(index): " + names);
        //remove(object)
        names.remove("Tom");
        System.out.println("After remove(object): " + names);
        //contains()
        System.out.println("Contains Robert? " + names.contains("Robert"));
        //size()
        System.out.println("Size: " + names.size());
        //isEmpty()
        System.out.println("Is empty? " + names.isEmpty());
        //indexOf()
        System.out.println("Index of Andrew: " + names.indexOf("Andrew"));
        //addAll()
        ArrayList<String> moreNames = new ArrayList<>();
        moreNames.add("John");
        moreNames.add("David");
        names.addAll(moreNames);
        System.out.println("After addAll(): " + names);
        //lear()
        names.clear();
        System.out.println("After clear(): " + names);
        //isEmpty() after clear
        System.out.println("Is empty? " + names.isEmpty());
    }
}
Compile and Run

Output:

After add(): [Tom, Robert, Andrew]
After add(index, element): [Tom, Jack, Robert, Andrew]
Element at index 2: Robert
After set(): [Tom, Jack, Mary, Andrew]
After remove(index): [Tom, Mary, Andrew]
After remove(object): [Mary, Andrew]
Contains Robert? false
Size: 2
Is empty? false
Index of Andrew: 1
After addAll(): [Mary, Andrew, John, David]
After clear(): []
Is empty? true