Thursday, October 9, 2025
HomeLanguagesJavaArrayList isEmpty() in Java with example

ArrayList isEmpty() in Java with example

The isEmpty() method of ArrayList in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.

Syntax:

list_name.isEmpty()

Parameter: It does not accepts any parameter.

Returns: It returns True if the list list_name has no elements else it returns false. The return type is of datatype boolean.

Error and Exceptions: This method has no error or exceptions.

Program to demonstrate working of isEmpty() in Java:




// Java code to demonstrate the working of
// isEmpty() method in ArrayList
  
// for ArrayList functions
import java.util.ArrayList;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // creating an Empty Integer ArrayList
        ArrayList<Integer> arr = new ArrayList<Integer>(10);
  
        // check if the list is empty or not using function
        boolean ans = arr.isEmpty();
        if (ans == true)
            System.out.println("The ArrayList is empty");
        else
            System.out.println("The ArrayList is not empty");
  
        // addition of a element to the ArrayList
        arr.add(1);
  
        // check if the list is empty or not
        ans = arr.isEmpty();
        if (ans == true)
            System.out.println("The ArrayList is empty");
        else
            System.out.println("The ArrayList is not empty");
    }
}


Output:

The ArrayList is empty
The ArrayList is not empty
RELATED ARTICLES

Most Popular

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6713 POSTS0 COMMENTS
Nicole Veronica
11876 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS