Skip to main content

5. void clear()

5. void clear()
Removes all of the elements from this list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//This Example shows the use of isEmpty and Clear method of Arraylist
 
package com.spy.arraylist.method;
 
import java.util.ArrayList;
 
public class Clear 
{
 
	public static void main(String[] args) {
 
		System.out.println("Starting Program");
 
		ArrayList<String> emp_list=new ArrayList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
 
 
		//Clearing arraylist
		  emp_list.clear();
 
		System.out.println("After clearing Arraylist");
 
		//Retrieving Arraylist
		  for (String s : emp_list) 
			{
 
				System.out.println(s);
			}
 
 
		  //method returns true if this list contains no elements
 
		   boolean b=emp_list.isEmpty();
		   System.out.println("Arraylist Empty :"+b);
 
 
 
		 System.out.println("ending program");
	  }
}

Output:

1
2
3
4
Starting Program
After clearing Arraylist
Arraylist Empty :true
ending program