Java Examples - Clear elements of LinkedList

Advertisements


Problem Description:

How to delete many elements from a linkedList?

Solution:

Following example demonstrates how to delete many elements of linkedList using Clear() method.

import java.util.*;

public class Main {
   public static void main(String[] args) {
      LinkedList<String> lList = new LinkedList<String>();
      lList.add("one");
      lList.add("two");
      lList.add("three");
      lList.add("four");
      lList.add("five");
      System.out.println(lList);
      lList.subList(2, 4).clear();
      System.out.println(lList);
   }
}

Result:

The above code sample will produce the following result.

[one, two, three, four, five]                                                                                                          
[one, two, five] 


Advertisements
Advertisements