ArrayList is one of the most used data struc­tures in Java. It allows you to dy­nam­ic­ally modify and store a col­lec­tion of objects. In this tutorial, we introduce you to the syntax of Java ArrayList and explain when to use it.

What is the dif­fer­ence 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 Col­lec­tions Framework and is not available natively. Unlike arrays, it must be imported from the java.util library.

Ar­rayL­ists are a suitable choice when the length of a Java List may po­ten­tially 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 pre­defined set of primitive data types such as int, float, char or Boolean.

One drawback of Ar­rayL­ists is they can take longer to access. While arrays have a fixed con­tinu­ous block of memory, objects in Ar­rayL­ists are not stored con­tigu­ously. It is important to take the ad­vant­ages and dis­ad­vant­ages of the data struc­tures 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 cor­res­pond­ing 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 re­spect­ive 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

Ar­rayL­ists use the cor­res­pond­ing 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

Op­er­a­tions such as adding or removing elements are not performed on Ar­rayL­ists with Java operators, but via pre­defined methods. We’ll show you the most common ArrayList methods below.

Adding elements

After creating the ArrayList ‘colours’ (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> colours = new ArrayList<>();
        colours.add("blue");
        colours.add("red");
        colours.add("green");
        System.out.println("ArrayList: " + colours);
    }
}
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> colours = new ArrayList<>();
        colours.add("blue");
        colours.add("red");
        colours.add("green");
        System.out.println("ArrayList: " + colours);
        String colour = colours.remove(1);
        System.out.println("ArrayList: " + colours);
        System.out.println("Removed Element: " + colour);
    }
}
Java

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

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

As with most pro­gram­ming 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> colours = new ArrayList<>();
        colours.add("blue");
        colours.add("red");
        colours.add("green");
        System.out.println("ArrayList: " + colours);
        colours.set(2, "yellow");
        System.out.println("Modified ArrayList: " + colours);
    }
}
Java

We now see yellow instead of green at index 2:

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

De­term­in­ing the length of Java ArrayList

The number of elements in ArrayList can easily be cal­cu­lated using the .size() method.

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

This is the result:

3
Java

Sorting and iterating through ArrayList

To sort Java ArrayList, the col­lec­tions class must be imported. For the iteration, we use a Java for-each loop. For each iteration of the loop, the re­spect­ive 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

dc8b52a43898356b40686188ee9833ee

e55dd419814f58a4e87e39befe1f5071

f4923fd8ba30637c20154bb3a1e957db

1480612d3e8e40504c5d058a0c210e88

b1325df3e2dc7b9da8307052ded3fe1a

Go to Main Menu