Java ArrayList - isEmpty() Method
The java.util.ArrayList.isEmpty() method is used to check whether the ArrayList is empty or not. It returns true if the size of the ArrayList is zero, else returns false.
Syntax
public boolean isEmpty()
Parameters
No parameter is required.
Return Value
true if the ArrayList is empty, else returns false.
Exception
NA
Example:
In the example below, the java.util.ArrayList.isEmpty() method is used to check whether the ArrayList is empty or not.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating an ArrayList ArrayList<Integer> MyList = new ArrayList<Integer>(); //checking ArrayList System.out.println("Is MyList empty?: " + MyList.isEmpty()); //populating ArrayList MyList.add(10); MyList.add(20); MyList.add(30); //checking ArrayList System.out.println("Is MyList empty?: " + MyList.isEmpty()); } }
The output of the above code will be:
Is MyList empty?: true Is MyList empty?: false
❮ Java.util - ArrayList