清除 Java 中的 LinkedList
可以使用 java.util.LinkedList.clear() 方法清除 Java 中的 LinkedList。此方法会删除 LinkedList 中的所有元素。LinkedList.clear() 方法不要求具有任何参数,并且不会返回值。
一个对此进行演示的程序如下所示。
示例
import java.util.LinkedList;
public class Demo {
public static void main(String[] args) {
LinkedList<String> l = new LinkedList<String>();
l.add("Orange");
l.add("Apple");
l.add("Peach");
l.add("Guava");
System.out.println("LinkedList before using the LinkedList.clear() method: " + l);
l.clear();
System.out.println("LinkedList after using the LinkedList.clear() method: " + l);
}
}上述程序的输出如下所示
LinkedList before using the LinkedList.clear() method: [Orange, Apple, Peach, Guava] LinkedList after using the LinkedList.clear() method: []
现在让我们了解一下上述程序。
创建 LinkedList l,然后使用 LinkedList.add() 将元素添加到此 LinkedList 中。在使用 LinkedList.clear() 方法(该方法会清除 LinkedList)之前和之后显示 LinkedList。展示此过程的代码片段如下所示
LinkedList<String> l = new LinkedList<String>();
l.add("Orange");
l.add("Apple");
l.add("Peach");
l.add("Guava");
System.out.println("LinkedList before using the LinkedList.clear() method: " + l);
l.clear();
System.out.println("LinkedList after using the LinkedList.clear() method: " + l);
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP