For a Java ArrayList, use list.size() to get the number of elements currently in the list. For an array, use array.length to get the number of components. size() is a method; length is a field. An ArrayList’s storage capacity is separate from its element count.
ArrayList size vs. array length
| What you want to count | Java expression | What it means |
|---|---|---|
Elements in an ArrayList |
list.size() |
The elements currently in the list |
| Components in an array | array.length |
The number of components in that array object |
Backing storage for an ArrayList |
Not returned by size() |
Capacity is the internal storage available to the list and is at least its current size |
For example:
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
Both expressions produce 2 here, but they count different structures through different APIs. Oracle’s Java SE 26 ArrayList API defines size() as the number of elements in the list. The Java Language Specification, Java SE 26, §10.7 defines an array’s public final length field as the number of its components.
As an Amazon Associate I earn from qualifying purchases.
What changes when elements are added or removed?
An ArrayList’s size can change
An ArrayList is a resizable-array implementation of List. Adding or removing elements changes its current size, which size() reports.
An array’s length stays fixed
An array’s length does not change during that array object’s lifetime. You can make a variable refer to a different array, but that does not resize the original array.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Size is not ArrayList capacity
Capacity describes the size of the array an ArrayList uses internally to store elements. It is always at least the list’s current size and may grow as elements are added. The API does not specify a particular growth policy, so do not infer capacity from size().
For example, constructing a list with an initial capacity reserves storage; it does not mean the list already contains that many elements. The API documents ensureCapacity and trimToSize as capacity-related controls. Use them when managing storage is relevant, rather than treating capacity as the element count.
Quick Recap
Best Value
Rank #4
Rank #2
Use the right expression in checks and loops
- To check whether an
ArrayListis empty, prefer itsisEmpty()method. Usesize()when you need the count or a loop boundary. - For an array, use
array.length. An array does not providesize(); anArrayListdoes not expose an array-stylelengthfield. - A count is not the last valid index. With
nelements or components, valid indices run from0throughn - 1. A conventional loop therefore testsi < 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]);
}
Common mistakes to avoid
- Calling
array.size(): arrays use thelengthfield. - Writing
list.length: anArrayListusessize()to report its current element count. - Using an initial capacity as if it were the number of elements already present.
- Using
i <= countas a loop condition, which attempts to access one index beyond 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.




