How Remove All Elements From LinkedList Example

This examples show how to remove all elements from LinkedList in java.  This examples also shows how to delete all elements from LinkedList in Java using LinkedList clear() method.

LinkedList clear() Method Example in Java?

We will delete all elements from LinkedList using LinkedList clear() method. LinkedList clear() method removes all of the elements from this list. The list will be empty after this call returns.

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);

}

}

Output:

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

After Removing Linklist is = []

In this post we discuss how to remove all elements of LinkedList using clear() method.This example in part of LinkedList in Java .

 

Leave a Reply

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

+ 41 = 47