Home  >  Article  >  Java  >  Java Example - Delete elements from linked list

Java Example - Delete elements from linked list

黄舟
黄舟Original
2017-01-22 16:18:421514browse

The following example demonstrates the use of the Clear() method to delete elements in a linked list:

/*
 author by w3cschool.cc
 Main.java
 */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);
   }}

The output result of running the above code is:

[1, 8, 6, 4, 5][1, 8, 5]

The above is a Java example - deleting the contents of an element in a linked list. For more information, please visit Follow the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn