Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

List.size() — count the elements in a list

`List.size()` returns the number of elements in a list as an `int`. Use it to check whether there are enough elements or to set an exclusive upper bound for indexing.

Version: Java SE 21

Syntax

int size()

int n = list.size();

List.size is shorthand for the List.size() method of java.util.List in Java SE 21. It's a no-argument instance method: call list.size(), not List.size(). It returns an int and leaves the list unchanged.

An empty list has size 0. Each occurrence counts separately, including null elements if the list permits them. If the list has more than Integer.MAX_VALUE elements, the method returns Integer.MAX_VALUE. If list itself is null, the call throws NullPointerException.

Size is not internal capacity: new ArrayList<>(8) creates an empty list with an initial capacity of 8. For an array, read the array.length field without parentheses. ArrayList.size() runs in constant time, but List makes no such guarantee for every implementation.

Use list.isEmpty() if you only need to check for an empty list. The condition n >= 2 checks for at least two elements. With n = list.size(), use 0 <= i && i < n for list.get(i); i == n throws IndexOutOfBoundsException. A stored n does not update automatically: read the size again after it changes if you need the current bound.

Examples

import java.util.ArrayList;
import java.util.List;

public class ListSizeExample {
    public static void main(String[] args) {
        List<String> colors = new ArrayList<>(8);
        System.out.println("size = " + colors.size());

        colors.add("red");
        colors.add("blue");
        colors.add("red");

        int n = colors.size();
        System.out.println("size = " + n);
        if (n < 2) {
            return;
        }

        for (int i = 1; i < n; i++) {
            System.out.println(i + ": " + colors.get(i));
        }
    }
}

Result

size = 0
size = 3
1: blue
2: red

The program first prints `0` despite the initial capacity of 8. After three additions it prints `3` because both occurrences of `red` count. The `n < 2` guard would exit if there were no second element. The loop starts at index `1`, so it prints `blue` and the final `red`.

Related lessons

  • Java · Lesson 11 · Advanced · Version: 2
  • Java · Lesson 12 · Advanced · Version: 2
  • Java · Lesson 13 · Advanced · Version: 2
  • Java · Lesson 14 · Advanced · Version: 2
  • Java · Lesson 22 · Advanced · Version: 2
  • Java · Lesson 35 · Advanced · Version: 2
  • Java · Lesson 39 · Basic · Version: 2
  • Java · Lesson 54 · Basic · Version: 2
  • Java · Lesson 58 · Basic · Version: 2
  • Java · Lesson 62 · Basic · Version: 2
  • Java · Lesson 2 · Intermediate · Version: 2
  • Java · Lesson 6 · Intermediate · Version: 2
  • Java · Lesson 33 · Intermediate · Version: 2
  • Java · Lesson 34 · Intermediate · Version: 2
  • Java · Lesson 36 · Intermediate · Version: 2
  • Java · Lesson 41 · Intermediate · Version: 2
  • Java · Lesson 43 · Intermediate · Version: 2
  • Java · Lesson 44 · Intermediate · Version: 2
  • Java · Lesson 50 · Intermediate · Version: 2
  • Java · Lesson 57 · Intermediate · Version: 2

Sources