1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* Remove all elements or clear LinkedList Java example This java example shows how to all elements or clear or empty Java LinkedList using clear or removeAll method. */ import java.util.LinkedList; public class RemoveAllElementsLinkedListExample { public static void main(String[] args) { //create LinkedList object LinkedList lList = new LinkedList(); //add elements to LinkedList lList.add("1"); lList.add("2"); lList.add("3"); lList.add("4"); lList.add("5"); System.out.println("LinkedList contains : " + lList); /* * To remove all elements of Java LinkedList, use * void clear() method. * * This method remove all elements from LinkedList object. */ lList.clear(); System.out.println("LinkedList now contains : " + lList); /* * Please note that removeAll method can also be used to remove * all elements from LinkedList. */ } } /* Output would be LinkedList contains : [1, 2, 3, 4, 5] LinkedList now contains : [] */ |
Add Comment