The ArrayList.isEmpty() method in Java is used to check if a list is empty. An empty list means that it contains no elements.
The ArrayList.isEmpty() method can be declared as shown in the code below:
public boolean isEmpty()
The ArrayList.isEmpty() method returns a boolean such that:
true if the list is empty.false if the list is not empty.Consider the code below, which demonstrates the use of the ArrayList.isEmpty() method:
import java.util.ArrayList;public class Example1 {public static void main(String[] args){ArrayList<Integer> list1 = new ArrayList<Integer>(5);boolean empty = list1.isEmpty();System.out.println("list1 is empty: " + empty);list1.add(1);list1.add(2);list1.add(3);empty = list1.isEmpty();System.out.println("list1 is empty: " + empty);}}
list1 is declared in line 6.ArrayList.isEmpty() method is used in line 8 to check if list1 is empty. The ArrayList.isEmpty() returns true because list1 contains no elements.list1 in lines 11-13 using the add() method.ArrayList.isEmpty() method is used in line 15 to check if list1 is empty. The ArrayList.isEmpty() returns false because list1 now contains three elements.