The ArrayList.size() method returns the exact number of elements currently stored in the list as an int in O(1) constant time.
Related List Operations: Because ArrayList uses 0-based indexing, valid indices for ArrayList.get() and ArrayList.set() always run from 0 to size() - 1. To test whether size() == 0, prefer ArrayList.isEmpty(). To manage the underlying buffer capacity separately from size(), see ArrayList.ensureCapacity() and ArrayList.trimToSize().
Declared in java.util.ArrayList<E> (implementing Collection.size()):
// Returns the number of elements currently stored in this list
public int size()
| Aspect | Specification | Operational Rule |
|---|---|---|
| Return Value | int (>= 0) |
Number of elements currently present in the list (including duplicates and nulls). |
| Time Complexity | O(1) |
Returns the private int size counter directly without scanning the array. |
| Last Element Index | size() - 1 |
When size() > 0, the final element is always at list.get(list.size() - 1). |
Combining size() with a for loop and get(i) visits every element from index 0 up to size() - 1:
ArrayList<Integer> marks = new ArrayList<Integer>(Arrays.asList(55, 54, 55, 54));
System.out.println("Total marks count: " + marks.size()); // 4
ArrayList<String> languages = new ArrayList<>(Arrays.asList("Java", "PHP", "Python"));
for (int i = 0; i < languages.size(); i++) {
System.out.println(languages.get(i));
}Terminal Output:
Total marks count: 4
Java
PHP
Python
add() and addAll() increment size(), remove() and clear() decrement or reset size(), while set(index, val) overwrites an existing slot and leaves size() unchanged:
ArrayList<String> list = new ArrayList<>();
System.out.println("Initial size: " + list.size()); // 0
list.add("A");
list.add("B");
System.out.println("After 2 adds: " + list.size()); // 2
list.set(0, "A-Updated");
System.out.println("After set(0, ..): " + list.size()); // 2
list.remove(1);
System.out.println("After remove(1): " + list.size()); // 1Terminal Output:
Initial size: 0
After 2 adds: 2
After set(0, ..): 2
After remove(1): 1
Do not confuse size() with internal array capacity! Creating new ArrayList<>(50) allocates room for 50 items, but size() is still 0. Conversely, adding a null reference via items.add(null) does count as an element and increments size():
ArrayList<String> items = new ArrayList<>(50);
System.out.println("New with capacity 50 -> size(): " + items.size()); // 0
items.add(null);
items.add("Data");
System.out.println("After adding null + 'Data' -> size(): " + items.size()); // 2Terminal Output:
New with capacity 50 -> size(): 0
After adding null + 'Data' -> size(): 2
One of the most frequent compiler errors for Java developers is mixing up how arrays, strings, and collections report their size:
int[] rawArray = {10, 20, 30};
String text = "Java";
ArrayList<String> list = new ArrayList<>(Arrays.asList("X", "Y"));
System.out.println("int[].length: " + rawArray.length); // Field (no parentheses)
System.out.println("String.length(): " + text.length()); // Method: length()
System.out.println("ArrayList.size(): " + list.size()); // Method: size()Terminal Output:
int[].length: 3
String.length(): 4
ArrayList.size(): 2
Quick reference across arrays, strings, and ArrayList:
| Target Type | Syntax | What It Returns |
|---|---|---|
ArrayList<E> / Collection |
list.size() |
Number of logical elements currently stored in the collection. |
Java Array (T[], int[]) |
arr.length |
Fixed total slot capacity of the array (field, no parentheses). |
String / CharSequence |
str.length() |
Number of char code units in the string. |
Using Math.min(start + pageSize, ids.size()) with subList() slices a list into safe pages without overflowing the final partial page:
public class ListPaginator {
public static void main(String[] args) {
ArrayList<Integer> ids = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
int pageSize = 2;
for (int start = 0; start < ids.size(); start += pageSize) {
int end = Math.min(start + pageSize, ids.size());
System.out.println("Page: " + ids.subList(start, end));
}
}
}Terminal Output:
Page: [1, 2]
Page: [3, 4]
Page: [5]
Starting a loop at i = scores.size() - 1 and decrementing down to 0 allows you to call scores.remove(i) safely without skipping adjacent elements when indices shift:
public class ReverseLoopRemoval {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>(Arrays.asList(40, 85, 30, 95));
for (int i = scores.size() - 1; i >= 0; i--) {
if (scores.get(i) < 50) scores.remove(i);
}
System.out.println("Passing scores: " + scores);
}
}Terminal Output:
Passing scores: [85, 95]
Writing for (int i = 0; i <= list.size(); i++) runs one iteration too far and crashes with IndexOutOfBoundsException when list.get(list.size()) is called. Always use strict less-than: i < list.size().
If your loop adds or removes elements, a cached variable int n = list.size(); becomes stale immediately! Either evaluate list.size() dynamically in the loop condition or iterate backward.
Compile and run this verification class in any Java environment:
public class ArrayListSizeFullDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(20);
if (list.size() == 0 && list.isEmpty()) {
System.out.println("Check 1 Passed: Pre-sized list starts with size() == 0.");
}
list.addAll(Arrays.asList("One", "Two", "Three"));
if (list.size() == 3 && list.get(list.size() - 1).equals("Three")) {
System.out.println("Check 2 Passed: Last index is size() - 1 (2).");
}
System.out.println("All ArrayList.size() verification checks passed!");
}
}Terminal Output:
Check 1 Passed: Pre-sized list starts with size() == 0.
Check 2 Passed: Last index is size() - 1 (2).
All ArrayList.size() verification checks passed!
No. Because ArrayList.size() simply returns a field (return size;), the JVM JIT compiler inlines the method call completely at zero overhead.
No. The minimum return value of size() is 0 (when isEmpty() is true).
No. trimToSize() only shrinks the hidden backing array capacity down to match the current size(); the logical element count stays identical.
grid.size() returns the number of rows. To count all cells across all rows, sum row.size() for each inner list.
size() == 0 cleanly.size().0..size() - 1.ArrayList.size() becomes 0.ArrayList.ArrayList methods.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.