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.
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); } }
Given ArrayList : [Pineapple, Papaya, Mulberry, Apple, Banana, Cherry, Guava, Watermelon] Remove All Elements of an ArrayList at a Time : []
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions