在 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 情侣520的祝福语简短
16 求婚蛋糕卡片祝福语简短
17 苦行老师祝福语结婚简短
18 征集粉丝祝福语简短幽默