Java ArrayList size() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, size() returns the number of elements currently stored in an ArrayList. Every element counts, including duplicates and null values.

Arrays use the length field and strings use length(), but lists use size(). Valid indexes run from 0 to size() - 1. The size is not the same as the capacity, the room the list has reserved internally.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

No arguments; returns an int.

Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
int n = list.size();               // 3
String last = list.get(n - 1);     // "c"
CallWhat it does
list.size()Number of elements.
list.get(list.size() - 1)The last element (the list must not be empty).
for (int i = 0; i < list.size(); i++)Loops over every valid index.
list.size() == 0Same as list.isEmpty().

size() runs in constant time: ArrayList keeps a counter and does not need to walk the elements.

Library declarations
Java
// java.util.ArrayList<E>
public int size()

// Declared in Collection<E> and List<E>

Parameters

ParameterDescription
Nonesize() takes no parameters.

Return Value

Return ValueDescription
intThe number of elements in the list (0 or more).

Compare with Similar Methods

Size-related methods:

MethodHow it differs
isEmpty()true when the size is 0.
ensureCapacity(n)Reserves room; does not change the size.
trimToSize()Shrinks the capacity down to the size.
clear()Sets the size back to 0.

Java Examples

Example 1 — Watch size() Change

Add four items, including a duplicate and a null, then remove one.

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

public class ArrayListSizeDemo {
    public static void main(String[] args) {
        List<String> bag = new ArrayList<>();
        System.out.println("start: " + bag.size());

        bag.add("book");
        bag.add("pen");
        bag.add("book");
        bag.add(null);
        System.out.println("after 4 adds: " + bag.size());

        bag.remove("book");
        System.out.println("after remove: " + bag.size() + " " + bag);
    }
}

How It Works

  1. Empty start. A new ArrayList has size 0.
  2. Everything counts. The repeated "book" and the null each count as one element, so the size is 4.
  3. One removed. remove("book") deletes only the first "book", so the size drops by exactly one.
  4. Order kept. The remaining elements shift left and stay in order: pen, book, null.

Example 2 — Initial Capacity Is Not Size

Create a list with room for 100, try to set index 0, then fill it past its capacity.

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

public class ArrayListSizeCapacity {
    public static void main(String[] args) {
        List<Integer> scores = new ArrayList<>(100);
        System.out.println("capacity 100, size " + scores.size());

        try {
            scores.set(0, 7);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("set(0, 7) failed: size is " + scores.size());
        }

        for (int i = 0; i < 150; i++) {
            scores.add(i);
        }
        System.out.println("after 150 adds: " + scores.size());
    }
}

How It Works

  1. Room, not elements. The constructor argument 100 is the initial capacity; the list still holds nothing.
  2. No slot to replace. set(0, 7) needs an existing element at index 0, so it throws IndexOutOfBoundsException.
  3. Grows by itself. Adding 150 numbers goes past the reserved 100; the list quietly grows its internal array when needed.
  4. Only adds count. The final size, 150, is exactly the number of add calls.

Example 3 — Use size() to Find the Middle and the End

Read the first, middle, and last names, then take the first three safely.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListSizeIndexes {
    public static void main(String[] args) {
        List<String> team = new ArrayList<>(Arrays.asList("Ari", "Bex", "Col", "Dan", "Eve"));
        int n = team.size();

        System.out.println("first: " + team.get(0));
        System.out.println("middle: " + team.get(n / 2));
        System.out.println("last: " + team.get(n - 1));
        System.out.println("top 3: " + team.subList(0, Math.min(3, n)));
    }
}

How It Works

  1. Store the size. n is 5, read once and reused.
  2. Middle index. Integer division gives 5 / 2 = 2, the index of Col.
  3. Last index. Indexes stop one short of the size, so the last element is at n - 1 = 4.
  4. Safe limit. Math.min(3, n) keeps the end index within the size, so the same line would also work for a list with fewer than three names.

Frequently Asked Questions

Call list.size(). ArrayList has no length field or length() method.
Size is the number of elements stored. Capacity is how many the internal array can hold before it must grow. Only size is visible through the API.
Yes. Every stored element counts, including null and repeated values.
size() - 1. An empty list has no valid index.
No. It returns a stored counter, so it takes the same time for any list size.

Next: sort()

Sort the list in place with a Comparator (Java 8+).

sort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful