how to check arraylist is empty or not code example
Example 1: ArrayList isEmpty() method in java
import java.util.ArrayList;
public class ArrayListIsEmptyMethodExample
{
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>();
System.out.println("Is ArrayList empty: " + al.isEmpty());
al.add(86);
al.add(25);
al.add(53);
al.add(85);
System.out.println("Is ArrayList empty: " + al.isEmpty());
for(Integer num : al)
{
System.out.println(num);
}
}
}
Example 2: validation list empty java
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>();
System.out.println(list.isEmpty());
list.add("A");
System.out.println(list.isEmpty());
list.clear();
System.out.println(list.isEmpty());
}
}