Java 25 (LTS) reference · List and ArrayList Methods

Java List size() Method

Return the number of elements in a list. isEmpty() is a clearer way to check for zero.

Java 25 (LTS) Since Java 1.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

list.size()

Return value

An int.

Examples

Count and clear
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> queue = new ArrayList<>(List.of("a", "b"));
        System.out.println(queue.size());
        queue.clear();
        System.out.println(queue.size() + " " + queue.isEmpty());
    }
}

Output

2
0 true

Run this on your computer — see “Get Started” for setup.

Numbered output
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> names = List.of("Ana", "Ben", "Cleo");
        for (int i = 0; i < names.size(); i++) System.out.println((i + 1) + ". " + names.get(i));
    }
}

Output

1. Ana
2. Ben
3. Cleo

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 1.0Added

Was this page helpful?