Java.util.LinkedList.addAll() 方法
描述
java.util.LinkedList.addAll(Collection<? extends E> c) 方法将指定集合中的所有元素附加到此列表的末尾,按照指定集合的迭代器返回它们的顺序。
声明
以下是 java.util.LinkedList.addAll() 方法的声明
public boolean addAll(Collection<? extends E> c)
参数
c − 包含要添加到此列表中的元素的集合
返回值
如果此列表因调用而更改,则此方法返回 true
异常
NullPointerException − 如果指定集合为空
示例
下面的例子展示了 java.util.LinkedList.addAll() 方法的使用。
package com.tutorialspoint;
import java.util.*;
public class LinkedListDemo {
public static void main(String[] args) {
// create a LinkedList
LinkedList list = new LinkedList();
// add some elements
list.add("Hello");
list.add(2);
list.add("Chocolate");
list.add("10");
// print the list
System.out.println("LinkedList:" + list);
// create a new collection and add some elements
Collection collection = new ArrayList();
collection.add("One");
collection.add("Two");
collection.add("Three");
// append the collection in the LinkedList
list.addAll(collection);
// print the new list
System.out.println("LinkedList:" + list);
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Hello, 2, Chocolate, 10, One, Two, Three]

