Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use list.size() to get the number of elements in a Java ArrayList; use array.length to get the number of components in a Java array. size() is a method, while length is a field. Neither expression reports an ArrayList‘s backing-storage capacity.

What does each value count?

An ArrayList is a resizable implementation of Java’s List interface. Its size() method reports how many elements are currently in the list. An array’s length field reports how many components that particular array contains. Oracle’s Java SE 26 ArrayList API documents the list method; the Java Language Specification, Java SE 26 Edition defines the array field.

What you need Java expression What it counts
ArrayList element count list.size() Elements currently in the list
Array component count array.length Components in that array object
ArrayList backing capacity Not returned by size() Internal storage capacity, which is at least the list’s element count

How do you get the count in code?

These expressions can produce the same number when the list and array happen to contain the same number of values, but they query different objects through different APIs:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import java.util.ArrayList;

ArrayList<String> names = new ArrayList<>();
names.add("Ada");
names.add("Linus");

int listElementCount = names.size(); // 2

String[] fixedNames = {"Ada", "Linus"};
int arrayComponentCount = fixedNames.length; // 2

Why is ArrayList capacity different from size?

An ArrayList uses an array internally, but its capacity is not its element count. Capacity describes the storage available for list elements; it is at least the current size and can grow as elements are added. The Java SE 26 API does not specify a precise growth policy. Constructing a list with an initial capacity reserves storage; it does not mean that the list already contains that many elements.

The API documents ensureCapacity and trimToSize for capacity management. These concern the internal storage, not the value returned by size(). Use size() when your question is how many elements are in the list.

Does the count change?

ArrayList size can change

Adding or removing elements changes the number of elements in the list, so a later call to size() can return a different value.

An array’s length is fixed

An array’s length does not change during that array object’s lifetime. A variable can be reassigned to refer to another array, but that creates a different referenced array object; it does not resize the original one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which expression should you use?

  • For an ArrayList or another list, call size(). For a direct empty check, use isEmpty().
  • For an array, read its length field.
  • Do not write array.size() or list.length: those are not the respective APIs for arrays and lists.
  • Do not use a list’s initial capacity as its element count.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do you avoid an off-by-one error?

A count is not the last valid index. If an array or list contains n items, its valid indices run from 0 through n - 1. A conventional loop therefore tests whether the index is less than the count:

for (int i = 0; i < names.size(); i++) {
    System.out.println(names.get(i));
}

for (int i = 0; i < fixedNames.length; i++) {
    System.out.println(fixedNames[i]);
}

Using <= instead of < would attempt to access index n, which is outside the valid range.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.