The size()
method is a fundamental operation in the Java ArrayList
class. It allows you to determine the number of elements currently stored in an ArrayList, making it a crucial tool for iteration, validation, and working with dynamic data collections.
What Is the size() Method in ArrayList?
- The
size()
method returns the number of elements (items) present in theArrayList
. - It does not count the internal capacity—only the actual, current number of list elements.
- The method is efficient, operating in constant time O(1) regardless of list size.
Syntax
public int size()
Parameters
- The
size()
method does not take any parameters.
Return Value
Returns | Description |
---|---|
int | The number of elements in the list |
Exceptions
- The method does not throw any exceptions for standard
ArrayList
usage. - Always supported in standard implementations.
How Does size() Work Internally?
- Internally,
ArrayList
maintains a count of added and removed elements. size()
simply returns this count—no element traversal or calculation each time.- Calling
size()
is very fast and safe to use even in loops or intensive operations.
Examples of the size() Method
1. Getting List Size
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); System.out.println("Number of names: " + names.size()); // Output: 3 } }
2. Use size() in a for Loop
import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(5); numbers.add(10); numbers.add(15); for (int i = 0; i < numbers.size(); i++) { System.out.println("Element at index " + i + ": " + numbers.get(i)); } } }
Output:
Element at index 0: 5
Element at index 1: 10
Element at index 2: 15
3. Check If an ArrayList Is Empty Using size() method
import java.util.ArrayList; public class Example { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); if (list.size() == 0) { System.out.println("The list is empty."); } } }
(Prefer isEmpty()
for clarity, but this approach is also valid.)
4. Size After Adding and Removing Elements
import java.util.ArrayList; public class SizeTest { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); System.out.println("Before removal: " + fruits.size()); // Output: 2 fruits.remove("Banana"); System.out.println("After removal: " + fruits.size()); // Output: 1 } }
Important Notes
- The
size()
method reflects current list contents only—not initial capacity or maximum size. - Adding or removing elements dynamically updates the value returned by
size()
. - Useful for validating index ranges: valid indexes are from
0
tosize()-1
. - Use
size()
in loop boundaries and input validation. - For empty lists,
size()
returns0
.
Summary
Aspect | Details |
---|---|
Method Purpose | Get the current number of elements |
Syntax | list.size() |
Return Value | int (number of elements) |
Parameters | None |
Throws Exception | No (in standard usage) |
Typical Use Cases | Looping, validation, data structure checks |
Typical Use Cases
- Looping over all elements: Safely bounded for-loops and index-based traversals.
- Empty check: Combined with
== 0
or in validation. - Dynamic collections: Monitor and react to changes in list content.
- User interface updates: Displaying counts, inventory, or summary statistics.