Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ArrayList isEmpty() method
Syntax and Examples


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? true

Here, 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? false

In 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
Bob

The 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.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M