ADVERTISEMENT
ADVERTISEMENT

Java program to remove all elements from an ArrayList

In this Java program, we are going to learn how to remove all elements from an ArrayList? Here, we are using ArrayList.clear() to remove all elements.
Submitted by IncludeHelp, on October 21, 2017

Given an ArrayList, and we have to remove/clear all elements using Java program.

ArrayList.clear()

This method is used to clear/remove all elements from an ArrayList.

In this program, we have 5 elements which have been added in the ArrayList, the elements are 100, 200, 300, 400 and 500. So, total numbers of elements are 5 and after removing all the elements ArrayList, total number of elements will be 0.

Consider the program

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

Java ArrayList Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.