在 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 妈妈生日祝福语简短励志