Easy Tutorial
❮ Number Min Java String Charat ❯

Java ArrayList isEmpty() Method

Java ArrayList

The isEmpty() method is used to check if the dynamic array is empty.

The syntax for the isEmpty() method is:

arraylist.isEmpty()

Note: arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

Returns true if the array contains no elements.

Returns false if the array contains elements.

Example

Check if the dynamic array is empty:

Example

import java.util.ArrayList;

class Main {
    public static void main(String[] args){

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();

        // Check if the array contains any elements
        boolean result = sites.isEmpty(); // true
        System.out.println("Is sites empty? " + result);

        sites.add("Google");
        sites.add("tutorialpro");
        sites.add("Taobao");
        System.out.println("Website list: " + sites);

        // Check if the array is empty
        result = sites.isEmpty();  // false
        System.out.println("Is sites empty? " + result);
    }
}

Executing the above program outputs:

Is sites empty? true
Website list: [Google, tutorialpro, Taobao]
Is sites empty? false

In the above example, we create an array named sites and use the isEmpty() method to check if it contains any elements.

Initially, the newly created array contains no elements. Therefore, isEmpty() returns true. After adding elements (Google, tutorialpro, etc.), the method returns false.

Java ArrayList

❮ Number Min Java String Charat ❯