Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Remove all elements from LinkedList - Testingpool

Remove all elements from LinkedList

In the previous post, we have seen how to remove First and Last element from the LinkedList. In this post, we will see how to remove all elements from the linkedList.

We use the method clear() for removing all the elements from the LinkedList.

Syntax:

public void clear(): Removes all elements from the list.

Example:

import java.util.LinkedList;

public class LinkedListEx {

	public static void main(String[] args) {
		LinkedList<String> fruits1 = new LinkedList<String>();
		fruits1.add("Orange");
		fruits1.add("Mango");
		fruits1.add("Apple");
		fruits1.add("Grapes");
		
		System.out.println("Before calling clear() method :"+ fruits1); //Remove Orange

		fruits1.clear();
		System.out.println("After calling clear() method :"+ fruits1);  //Remove Grapes
	}
}

Output:

Before calling clear() method :[Orange, Mango, Apple, Grapes]
After calling clear() method :[]


 

Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...