Write a Java program to test an array list is empty or not


The program first creates an ArrayList called list_col and adds some elements to it. Then it prints out the ArrayList using System.out.println().

Next, it checks if the ArrayList is empty or not by calling the isEmpty() method on the list_col object. This method returns a boolean value indicating whether the ArrayList is empty or not, which is then printed out using System.out.println().

After that, the program removes all the elements from the ArrayList using the removeAll() method. This method takes another ArrayList as a parameter and removes all the elements in the first ArrayList that are also present in the second ArrayList. In this case, we pass list_col as the parameter to remove all elements from itself.

Finally, the program checks again if the ArrayList is empty or not using the isEmpty() method and prints out the result.

Source Code

import java.util.ArrayList;
import java.util.Collections;
public class Test_ArrayList
{
	public static void main(String[] args)
	{
		ArrayList<String> list_col= new ArrayList<String>();
		list_col.add("Pink");
		list_col.add("Yellow");
		list_col.add("Black");
		list_col.add("White");
		list_col.add("Blue");
		list_col.add("Orange");
		System.out.println("Given Array List : " + list_col);
		System.out.println("Checking the above Array List is Empty or Not : "+list_col.isEmpty());
		list_col.removeAll(list_col);
		System.out.println("After Remove all Elements in Array List : "+list_col);   
		System.out.println("Checking the above Array List is Empty or Not  : "+list_col.isEmpty());
	}
}

Output

Given Array List : [Pink, Yellow, Black, White, Blue, Orange]
Checking the above Array List is Empty or Not : false
After Remove all Elements in Array List : []
Checking the above Array List is Empty or Not  : true

Example Programs