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.
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.
Removes a specified element from an ArrayList.
Replaces the element present in a specified position in an ArrayList.
Returns the number of elements in the ArrayList.

All contributors

Looking to contribute?

Learn Java on Codecademy