ArrayList size() method - Syntax and Examples
Learn about the Java ArrayList's size() method, its syntax, parameters, return value, and practical examples.
This reference explains ArrayList size() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
ArrayList size() method - Syntax and Examples
Introduction
The size() method in Java's ArrayList is a simple but essential tool. It tells you how many elements are currently stored within the list. Think of it like asking, 'How many items are in my shopping basket?' This information can be crucial for iterating through the list correctly, checking if it's empty, or performing calculations based on its contents.
Syntax
public int size()
Parameters
The size() method doesn't accept any parameters. It simply retrieves the current number of elements.
| Parameter | Description |
|---|---|
| None | The method does not take any input parameters. |
Return Value
The size() method returns an integer representing the number of elements currently in the ArrayList.
Examples
Example 1: Getting the Size of a List
This example demonstrates how to create an ArrayList and use the size() method to determine its initial size, then add elements and check the updated size.
import java.util.ArrayList;
public class SizeExample1 {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList<String> names = new ArrayList<>();
// Get the initial size
int initialSize = names.size();
System.out.println("Initial size: " + initialSize);
// Add some elements
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Get the updated size
int currentSize = names.size();
System.out.println("Current size: " + currentSize);
}
Initial size: 0
Current size: 3
In this example, we first create an empty ArrayList called names. The initial size is 0 because there are no elements in the list. We then add three names to the list and check the size again, which now correctly reflects the number of elements (3).
Example 2: Checking if a List is Empty
This example shows how you can use the size() method to determine whether an ArrayList is empty.
import java.util.ArrayList;
public class SizeExample2 {
public static void main(String[] args) {
// Create an ArrayList of integers
ArrayList<Integer> numbers = new ArrayList<>();
// Check if the list is empty
if (numbers.size() == 0) {
System.out.println("The list is empty.");
} else {
System.out.println("The list is not empty.");
}
}
The list is empty.
Since the numbers ArrayList is initially empty, its size is 0. The if statement evaluates to true, and the message "The list is empty." is printed.
Example 3: Iterating through an ArrayList
This example demonstrates how you can use the size() method within a loop to safely iterate over all elements in an ArrayList. It's important to know the size so that your loop doesn’t go out of bounds.
import java.util.ArrayList;
public class SizeExample3 {
public static void main(String[] args) {
// Create an ArrayList of characters
ArrayList<Character> letters = new ArrayList<>();
letters.add('A');
letters.add('B');
letters.add('C');
// Iterate through the list using size()
for (int i = 0; i < letters.size(); i++) {
System.out.println("Element at index " + i + ": " + letters.get(i));
}
}
Element at index 0: A
Element at index 1: B
Element at index 2: C
The loop iterates from i = 0 to i < letters.size(), ensuring that we access each element within the bounds of the ArrayList. Using `letters.size()` in the loop condition prevents an IndexOutOfBoundsException.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.