adsense


Showing posts with label JAVA EXAMPLES CLEAR ELEMENTS OF LINKED LIST. Show all posts
Showing posts with label JAVA EXAMPLES CLEAR ELEMENTS OF LINKED LIST. Show all posts

Friday, 30 October 2015

JAVA EXAMPLES CLEAR ELEMENTS OF LINKED LIST PAGE 153

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES CLEAR ELEMENTS OF LINKED LIST








Java Examples - Clear elements of Linked List



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("1");
      lList.add("8");
      lList.add("6");
      lList.add("4");
      lList.add("5");
      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, three, Replaced, five]