Home Java ArrayList Methods size() Java ArrayList size() Method 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.
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
int n = list.size(); // 3
String last = list.get(n - 1); // "c" Call What 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.util.ArrayList<E>
public int size()
// Declared in Collection<E> and List<E> Parameters Parameter Description None size() takes no parameters.
Return Value Return Value Description intThe number of elements in the list (0 or more).
Compare with Similar Methods Size-related methods:
Java Examples 1. Count elements 2. Size vs capacity 3. Index math Example 1 — Watch size() Change Add four items, including a duplicate and a null, then remove one.
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);
}
} start: 0
after 4 adds: 4
after remove: 3 [pen, book, null] How It Works Empty start. A new ArrayList has size 0.Everything counts. The repeated "book" and the null each count as one element, so the size is 4.One removed. remove("book") deletes only the first "book", so the size drops by exactly one.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.
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());
}
} capacity 100, size 0
set(0, 7) failed: size is 0
after 150 adds: 150 How It Works Room, not elements. The constructor argument 100 is the initial capacity; the list still holds nothing.No slot to replace. set(0, 7) needs an existing element at index 0, so it throws IndexOutOfBoundsException.Grows by itself. Adding 150 numbers goes past the reserved 100; the list quietly grows its internal array when needed.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.
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)));
}
} first: Ari
middle: Col
last: Eve
top 3: [Ari, Bex, Col] How It Works Store the size. n is 5, read once and reused.Middle index. Integer division gives 5 / 2 = 2, the index of Col.Last index. Indexes stop one short of the size, so the last element is at n - 1 = 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 How do I get the length of an ArrayList in Java? Call list.size(). ArrayList has no length field or length() method.
What is the difference between size and capacity? 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.
Does size() count null elements and duplicates? Yes. Every stored element counts, including null and repeated values.
What is the last valid index of an ArrayList? size() - 1. An empty list has no valid index.
Is size() slow on a large ArrayList? No. It returns a stored counter, so it takes the same time for any list size.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion