How to remove all the elements from the LinkedList?

How to remove all the elements from the LinkedList?

Previous Home Next

 

This example will shows how to remove all the elements from the LinkedList .And check whether it is empty or not

To remove all the elements from the LinkedList we use clear() .
 clear() makes the list empty .and isEmpty () method check whether the list is empty or not.

Call the clear method through the object of the LinkedList .

 

package Example;
import java .util.LinkedList;
import java .util.Iterator;
public class LinklistExample {

/**
* @param args
*/
public static void main(String[] args) {
LinkedList Lst= new LinkedList(); // create object of LinkedList

// add elements to the LinkedList using add method
Lst.add("4");
Lst.add("6");
Lst.add("8");
Lst.add("10");
Lst.add("12");
Lst.add("14");
Lst.add("16");
Lst.add("18");
System.out.println("The elements of LinkedList prior to make it clear:"+Lst);

Lst.clear();
System.out.println("The linklist is empty:"+Lst.isEmpty());

The Linklist is empty:True
Previous Home Next