Java Program to Remove All Elements From an ArrayList

You are currently viewing Java Program to Remove All Elements From an ArrayList

Java program to remove all elements from an ArrayList

import java.util.ArrayList;

public class RemoveAllElements{
	public static void main(String []args){
		//ArrayList object
		ArrayList arrList = new ArrayList();

		//Add elements to Arraylist
		arrList.add("100");
		arrList.add("200");
		arrList.add("300");
		arrList.add("400");
		arrList.add("500");

		System.out.println("Total number of elements in ArrayList: "
			+ arrList.size());
		//remove all elements using clear() method
		arrList.clear();
		System.out.println("Total number of elements in ArrayList: "
			+ arrList.size());
	}
}

Output

Total number of elements in ArrayList: 5
Total number of elements in ArrayList: 0

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply