ArrayList (Java)

An ArrayList is a resizable, list-like collection in Java that can grow and shrink as you add or remove items. Unlike a plain array, it has no fixed size.

Java

ArrayList lives in the java.util package and stores objects, so you specify the element type in angle brackets, like ArrayList<String>. Common methods include add(), get(), remove(), and size().

It is one of the most-used classes in Java for holding ordered lists of items when the count isn't known ahead of time.

Examples

Java

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("apple");
        fruits.add("banana");
        System.out.println(fruits.get(0) + " " + fruits.size());
    }
}

Output:

apple 2

Learn it in a lesson

See also

Back to the Dictionary