Write a Java program to Remove all elements of an ArrayList at a time


This code creates an ArrayList called fru_list with several String elements, prints the original list, removes all elements from the list using the clear() method, and then prints the empty list. This code demonstrates how to remove all elements from an ArrayList using the clear() method.

Source Code

import java.util.ArrayList;
public class Remove_AllElements
{
    public static void main(String[] args)
    {
		ArrayList<String> fru_list = new ArrayList<String>();
		fru_list.add("Pineapple"); 
		fru_list.add("Papaya");
		fru_list.add("Mulberry");
		fru_list.add("Apple");
		fru_list.add("Banana");
		fru_list.add("Cherry");
		fru_list.add("Guava");
		fru_list.add("Watermelon");
		System.out.println("Given ArrayList : "+fru_list);
 
		//Removing all elements of the fru_list 
		fru_list.clear();
 
		System.out.println("Remove All Elements of an ArrayList at a Time : "+fru_list);
    }
}

Output

Given ArrayList : [Pineapple, Papaya, Mulberry, Apple, Banana, Cherry, Guava, Watermelon]
Remove All Elements of an ArrayList at a Time : []

Example Programs