Java.util.LinkedList.addAll() 方法
描述
java.util.LinkedList.addAll(int index,Collection<? extends E> c) 方法将指定集合中的所有元素插入到此列表中,从指定位置开始。
声明
以下是 java.util.LinkedList.addAll() 方法的声明
public boolean addAll(int index,Collection<? extends E> c)
参数
index − 插入指定集合中第一个元素的索引
c − 包含要添加到此列表的元素的集合
返回值
如果此列表因调用而更改,则此方法返回 true
异常
NullPointerException − 如果指定集合为空
IndexOutOfBoundsException − 如果索引超出范围
示例
下面的例子展示了 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");
// add the collection in the LinkedList at index 2
list.addAll(2, collection);
// print the new list
System.out.println("LinkedList:" + list);
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Hello, 2, One, Two, Three, Chocolate, 10]

