How to remove All element from LinkedList

package JavaLinkedListExample;

import java.util.LinkedList;
import java.util.Scanner;

 * Here we will learn to remove all element from Linked List
public class RemoveAllElement {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(;

		LinkedList<String> linkedList = new LinkedList<String>();
		String objInput;

		System.out.println("Insert the elements in List(Enter exit to quit)");
		objInput =;

		 * equalsIgnoreCase method compares this String to another String,
		 * ignoring case considerations.
		while (!objInput.equalsIgnoreCase("exit")) {
			objInput =;
		System.out.println("nElements in List");
		for(Object obj:linkedList)
		System.out.println("nSize of the list:"+linkedList.size());

		System.out.println("Clearing down the Linked List using Clear Method");
		System.out.println("Linked List Cleared");
		System.out.println("Size of the list after removing all element : "+linkedList.size());
		System.out.println("nNote*: linkedList.removeAll(linkedList) method can also be used to clear the LinkedList");



By | 2012-09-25T03:32:49+00:00 September 25th, 2012|Code Base|0 Comments

About the Author:

Leave A Comment