7. void clear()

7. 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 LinkedList
 
package com.spy.method.LinkedList;
 
import java.util.LinkedList;
 
public class Clear 
{
 
	public static void main(String[] args) {
 
		System.out.println("Starting Program");
 
		LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
 
 
		//Clearing LinkedList
		  emp_list.clear();
 
		System.out.println("After clearing LinkedList");
 
		//Retrieving LinkedList
		  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("LinkedList Empty :"+b);
 
 
		   System.out.println("ending program");
	  }
}

output

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

Leave a Reply

Your email address will not be published. Required fields are marked *