Clear an Arraylist Example Java Program


Definition

An ArrayList is a non-syncronized class. It implements List Interface and inherits AbstractList class. It maintains insertion order.

Syntax

arraylist.clear();

Clear ArrayList Example Program

import java.util.ArrayList;

class ClearArrayListExample{
	public static void main(String[] args){
		ArrayList<String> directionsArraylist = new ArrayList<String>();
		//Adding elements to arraylist
		directionsArraylist.add("NORTH");
		directionsArraylist.add("SOUTH");
		directionsArraylist.add("EAST");
		directionsArraylist.add("WEST");
		
		System.out.println("The arraylist is : "+directionsArraylist);
		System.out.println("The size of the arraylist is : "+directionsArraylist.size()); //Printing size of arraylist
		
		directionsArraylist.clear();//Clearing the arraylist or Removing all items in the arraylist
		
		System.out.println("The size of the arraylist after clearing is : "+directionsArraylist.size());//Printing size of arraylist
	}
}

Sample Output

The ArrayList is : [NORTH, SOUTH, EAST, WEST]
The size of the ArrayList is: 4
The size of the ArrayList after clearing is : 0