How to use Java ArrayList

ArrayList is one of the most used data structures in Java. It allows you to dynamically modify and store a collection of objects. In this tutorial, we introduce you to the syntax of Java ArrayList and explain when to use it.

What is the difference between ArrayList and Array in Java?

Java ArrayList is dynamic, which means that it grows and shrinks in size when elements are added or removed. It is worth noting that the ArrayList class is part of the Java Collections Framework and is not available natively. Unlike arrays, it must be imported from the java.util library.

ArrayLists are a suitable choice when the length of a Java List may potentially vary. Examples include storing objects, searching or sorting data, and creating lists or queues.

In contrast, it’s not possible to change the size of an array. This means that you should ideally know the number of objects that the array will hold in advance. Arrays are suitable for managing a predefined set of primitive data types such as int, float, char or Boolean.

One drawback of ArrayLists is they can take longer to access. While arrays have a fixed continuous block of memory, objects in ArrayLists are not stored contiguously. It is important to take the advantages and disadvantages of the data structures into account so you are able to select the one that works best for your use case.

What is the syntax for Java ArrayList?

Before creating ArrayList, the corresponding class must be imported from the java.util library.

import java.util.ArrayList;
Java

The general syntax is:

ArrayList<Type> arrayList= new ArrayList<>();
Java

“Type” stands for the respective data type in Java ArrayList.

The next step is to create lists of strings and integers.

ArrayList<String> arrayList= new ArrayList<>();
Java
ArrayList<Integer> arrayList= new ArrayList<>();
Java

ArrayLists use the corresponding wrapper class of primitive data types so that they are treated like objects. This means you have to specify integer instead of int.

Examples of Java ArrayList methods

Operations such as adding or removing elements are not performed on ArrayLists with Java operators, but via predefined methods. We’ll show you the most common ArrayList methods below.

Adding elements

After creating the ArrayList “colors” (String type), we’ll add various elements using the .add() method.

import java.util.ArrayList;
class Main {
    public static void main(String[] args){
        ArrayList<String> colors = new ArrayList<>();
        colors.add("blue");
        colors.add("red");
        colors.add("green");
        System.out.println("ArrayList: " + colors);
    }
}
Java

This results in:

ArrayList: [blue, red, green]
Java

Removing elements

To remove objects from Java ArrayList, we use the .remove() method and specify the element’s index.

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("blue");
        colors.add("red");
        colors.add("green");
        System.out.println("ArrayList: " + colors);
        String color = colors.remove(1);
        System.out.println("ArrayList: " + colors);
        System.out.println("Removed Element: " + color);
    }
}
Java

The output shows the modified ArrayList and the element that has been removed:

ArrayList: [blue, green]
Removed Element: red
Java

As with most programming languages, counting in Java starts from 0. This means that the removed element at index 1 is red.

Accessing elements in Java ArrayList

Using the function .get(), we can access an element at a specific position.

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> clothes = new ArrayList<>();
        clothes.add("jacket");
        clothes.add("shirt");
        clothes.add("pullover");
        System.out.println("ArrayList: " + clothes);
        String str = clothes.get(2);
        System.out.print("Element at index 2: " + str);
    }
}
Java

For the output we get:

ArrayList: [jacket, shirt, pullover]
Element at index 2: pullover
Java

Changing elements

With .set(), we can change an element by assigning a new element at a specific index.

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("blue");
        colors.add("red");
        colors.add("green");
        System.out.println("ArrayList: " + colors);
        colors.set(2, "yellow");
        System.out.println("Modified ArrayList: " + colors);
    }
}
Java

We now see yellow instead of green at index 2:

ArrayList: [blue, red, green]
Modified ArrayList: [blue, red, yellow]
Java

Determining the length of Java ArrayList

The number of elements in ArrayList can easily be calculated using the .size() method.

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("blue");
        colors.add("red");
        colors.add("green");
        System.out.println(colors.size());
    }
}
Java

This is the result:

3
Java

Sorting and iterating through ArrayList

To sort Java ArrayList, the collections class must be imported. For the iteration, we use a Java for-each loop. For each iteration of the loop, the respective element is output to the console.

import java.util.ArrayList;
import java.util.Collections;
public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> ages = new ArrayList<Integer>();
        ages.add(20);
        ages.add(54);
        ages.add(17);
        ages.add(9);
        Collections.sort(ages);
        for (int i : ages) {
            System.out.println(i);
        }
    }
}
Java

The elements of ArrayList are displayed from smallest to largest:

9
17
20
54
Java
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.