在 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);
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短