share

Monday, 18 May 2015

How Remove All Element From LinkedList Example

We can remove all element of LinkedList using clear() method . clear() removes all element from the list and return empty list.

 Method 1:

import java.util.LinkedList;

public class RemoveAll {

    public static void main(String[] args) {
       
        LinkedList<String> linkedList = new LinkedList<String>();
       
        linkedList.add("Mumbai");
        linkedList.add("Delhi");
        linkedList.add("Noida");
        linkedList.add("Gao");
        linkedList.add("Patna");
        //print linkedlist
        System.out.println("Before Removing Linklist is = "+linkedList);
       
        /*
         * Removes all of the elements from this list.
           The list will be empty after this call returns.
         */
       
        linkedList.clear();
       
        System.out.println("After Removing Linklist is = "+linkedList);

    }
   
}


Ouput:

Before Removing Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]

After Removing Linklist is = []


No comments:

Post a Comment