Write a Java program to empty an array list


This is a Java program that removes all elements from an ArrayList using the removeAll method. Here's how the program works:

  • The program creates an ArrayList of Strings, named str_col, and initializes it with some values.
  • The program prints the contents of str_col using System.out.println.
  • The program uses the removeAll method to remove all elements from str_col.
  • The program prints the contents of str_col again using System.out.println.

Source Code

import java.util.ArrayList;
import java.util.Collections;
public class Empty_ArrayList
{
	public static void main(String[] args)
	{
		ArrayList<String> str_col= new ArrayList<String>();
		str_col.add("Black");
		str_col.add("Orange");
		str_col.add("Pink");
		str_col.add("Blue");
		str_col.add("White");
		str_col.add("Yellow");	
		System.out.println("Original array list: " + str_col);
		str_col.removeAll(str_col);
		System.out.println("Array list after removing all elements "+ str_col);   
	}
}

Output

Original array list: [Black, Orange, Pink, Blue, White, Yellow]
Array list after removing all elements []

Example Programs