ArrayList isEmpty() method - Syntax and Examples
Java ArrayList isEmpty() method - Syntax and Examples
This reference explains ArrayList isEmpty() 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.
Introduction
The isEmpty() method in Java's ArrayList class is a straightforward way to check if an ArrayList contains any elements. It returns true if the list is empty (contains zero elements), and false otherwise. It’s often used as a preliminary check before attempting operations that might fail on an empty list, like trying to access its first element or iterate over it.
Syntax
public boolean isEmpty()
Parameters
| Parameter | Description |
|---|---|
| None | This method doesn't accept any parameters. |
Return Value
| Value | Description |
|---|---|
true |
If the ArrayList is empty (contains no elements). |
false |
If the ArrayList contains at least one element. |
Examples
Example 1: Checking an Empty ArrayList
This example demonstrates how to use isEmpty() with a newly created, empty ArrayList.
import java.util.ArrayList;
public class IsEmptyExample {
public static void main(String[] args) {
// Create an empty ArrayList
ArrayList names = new ArrayList<>();
// Check if the list is empty
boolean isEmpty = names.isEmpty();
// Print the result
System.out.println("Is the ArrayList empty? " + isEmpty);
}
}
Is the ArrayList empty? trueHere, we create an ArrayList called names. Since it's just created, it has no elements, so isEmpty() returns true.
Example 2: Checking a Non-Empty ArrayList
This example shows how isEmpty() behaves when the ArrayList contains elements.
import java.util.ArrayList;
public class IsNotEmptyExample {
public static void main(String[] args) {
// Create an ArrayList and add some elements
ArrayList numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
// Check if the list is empty
boolean isEmpty = numbers.isEmpty();
// Print the result
System.out.println("Is the ArrayList empty? " + isEmpty);
}
Is the ArrayList empty? falseIn this case, we add three integers to the numbers list. Because it now contains elements, isEmpty() returns false.
Example 3: Using isEmpty() before iteration
This example demonstrates a common use case: checking if an ArrayList is empty *before* attempting to iterate over it. This prevents potential errors like NullPointerException or unexpected behavior when dealing with empty lists in loops.
import java.util.ArrayList;
public class IsEmptyBeforeIteration {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList fruits = new ArrayList<>();
// Check if the list is empty before iterating
if (!fruits.isEmpty()) {
for (String fruit : fruits) {
System.out.println(fruit);
}
} else {
System.out.println("The list of fruits is empty.");
}
}
The list of fruits is empty.Because the fruits list is initially empty, the if (!fruits.isEmpty()) condition evaluates to false. The `else` block then executes and prints a message indicating that the list is empty.
Example 4: Using isEmpty() in a method
This example demonstrates how you can use isEmpty() inside a custom method to perform certain operations based on whether or not an ArrayList is empty. This promotes code reusability and makes your logic clearer.
import java.util.ArrayList;
public class IsEmptyInMethod {
// Method to print elements only if the list isn't empty
public static void printElements(ArrayList list) {
if (!list.isEmpty()) {
for (String item : list) {
System.out.println(item);
}
} else {
System.out.println("The list is empty, nothing to print.");
}
}
public static void main(String[] args) {
ArrayList numbers = new ArrayList<>();
printElements(numbers);
ArrayList names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
printElements(names);
}
}
The list is empty, nothing to print.
Alice
BobThe printElements method checks if the provided ArrayList is empty before attempting to iterate and print its contents. This keeps your code clean and prevents errors that could occur when working with potentially empty lists.
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.