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 -- 如果指定collection为null
-
IndexOutOfBoundsException -- 如果索引超出范围
例子
下面的示例演示java.util.LinkedList.addAll()方法的用法。
package com.yiibai; 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]