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(System.in);

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

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

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

		System.out.println("Clearing down the Linked List using Clear Method");
		linkedList.clear();
		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