在 Java 中清除 LinkedList
可以使用java.util方法在Java中清除LinkedList。.此方法删除LinkedList中的所有元素。该方法不需要参数,也不返回任何值。LinkedList.clear()LinkedList.clear()
演示此过程的程序如下所示。
示例
import java.util.LinkedList;
public class Demo {
public static void main(String[] args) {
LinkedList l = new LinkedList();
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: []
现在让我们了解上面的程序。
创建了LinkedListl。然后用于将元素添加到此LinkedList。LinkedList在使用清除LinkedList的方法之前和之后显示。演示这一点的代码片段如下LinkedList.add()theLinkedList.clear()
LinkedListl = new LinkedList (); 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);
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下