2:17 am - Saturday December 15, 2018

LinkedList example program to remove all elements using clear()

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

/**
*@author :CandidJava.com
*@descriptions : This program explain the function and use of clear() method
*/
import java.util.LinkedList;

class RemoveAllElementsLinkedList {
public static void main(String[] args) {
// creating LinkedList Object
LinkedList lList = new LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList contains : " + lList);
// Clear() method is used to remove all elements from the list
lList.clear();
System.out.println("LinkedList now contains : " + lList);
}
}

Output:

D:\CandidJava\Util\LinkedList>javac RemoveAllElementsLinkedList.java

Note: RemoveAllElementsLinkedList.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\LinkedList>java RemoveAllElementsLinkedList

LinkedList contains : [1, 2, 3, 4, 5]

LinkedList now contains : []

BACK

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply