Published Oct 28, 2021Updated Mar 21, 2022
Contribute to Docs

The ArrayList class uses dynamic arrays that are resizable, unlike traditional fixed arrays. However, each element must still be of the same type. Elements can be added or removed at any time, making the ArrayList more flexible.

Some other important points about the ArrayList class include:

  • It has the ability to contain duplicate elements.
  • It maintains insertion order.
  • It is non-synchronized and, therefore, not safe for multiple threading.
  • It allows random access since arrays work on an index basis.
  • The space/time complexity is a bit slower than a LinkedList due to the nature of adding/removing elements.


This class is imported with the java.util package.

import java.util.ArrayList;

The syntax for defining an instance of ArrayList is similar to defining a traditional array. A key difference is that angle brackets <> are used instead of square brackets:

ArrayList<DataType> arrayListInstance = new ArrayList<DataType>();


In the example below, a new ArrayList instance arr is defined. There are print statements throughout to show elements being added to and removed from arr before it is ultimately cleared:

// Import from the java.util package
import java.util.ArrayList;
public class Example {
public static void main(String []args) {
ArrayList<String> arr = new ArrayList<>();
System.out.println(arr); // Currently empty
System.out.println(arr.size()); // Prints size of ArrayList
System.out.println(arr); // Empty again

The output would be:

[hello, India]


Adds an element to an ArrayList.
Adds a collection to an ArrayList.
Checks if an element is present in the ArrayList or not.
Performs a specified action on each element in an ArrayList.
Retrieves the element present in a specified position in an ArrayList.
Returns the index of the first occurrence of a given element, or -1 if not found.
Checks if an ArrayList is empty.
Removes a specified element from an ArrayList.
Removes all the elements of an ArrayList that satisfy a given predicate.
Retains only the elements that are contained in the specified collection.
Replaces the element present in a specified position in an ArrayList.
Returns the number of elements in the ArrayList.
Used to sort arrays of primitive types and objects.
Returns a Spliterator over the elements in ArrayList.
Returns a view of a portion of the list based on the specified start and end indices.
Converts an ArrayList into an array.
Adjusts the capacity of the ArrayList to be the same as its size.

All contributors

Looking to contribute?

Learn Java on Codecademy