Java中的 CopyOnWriteArrayList addAll() 方法及示例
boolean addAll(集合 c)
此方法用于将集合c标识的所有元素附加到列表末尾,与集合的迭代器返回的顺序相同。
参数:此方法接受一个强制参数c ,它是包含元素的集合,这些元素将附加在列表的末尾。
返回值:此方法返回一个布尔值。如果集合正确插入到列表中,则返回 true,否则返回 false。
异常:如果指定元素为空,此方法将抛出NullPointerException 。
// Java Program to illustrate the
// CopyOnWriteArrayList.addAll(Collection c)
import java.io.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListDemo {
public static void main(String args[])
{
// create an empty array list1 with initial
// capacity as 5
CopyOnWriteArrayList list1
= new CopyOnWriteArrayList();
// use add() method to add elements in the list
list1.add(12);
list1.add(20);
list1.add(45);
// prints all the elements available in list1
System.out.println("Printing list1:");
for (Integer number : list1)
System.out.println("Number = " + number);
// create an empty array list2 with an initial
// capacity
CopyOnWriteArrayList list2
= new CopyOnWriteArrayList();
// use add() method to add elements in list2
list2.add(25);
list2.add(30);
list2.add(31);
list2.add(35);
// let us print all the elements available in
// list2
System.out.println("Printing list2:");
for (Integer number : list2)
System.out.println("Number = " + number);
// inserting all elements, list2 will get printed
// after list1
list1.addAll(list2);
System.out.println("Printing all the elements");
// let us print all the elements available in
// list1
for (Integer number : list1)
System.out.println("Number = " + number);
}
}
输出:
Printing list1:
Number = 12
Number = 20
Number = 45
Printing list2:
Number = 25
Number = 30
Number = 31
Number = 35
Printing all the elements
Number = 12
Number = 20
Number = 45
Number = 25
Number = 30
Number = 31
Number = 35
boolean addAll(int index, Collection c)
此方法是将一个集合的元素添加到另一个列表中。元素的插入将从指定的索引开始。元素将按照集合迭代器返回的顺序添加。添加新元素时,先前存在于该索引处的元素(如果有)将向右移动。
参数 :
index是集合元素将被插入的位置。
c是包含要添加到列表末尾的元素的集合。
返回值:它的返回类型是布尔值。如果集合正确插入到指定索引处的列表中,则返回 true,否则返回 false。
例外 :
NullPointerException – 如果指定的集合为空。
IndexOutOfBoundsException – 如果指定的索引超出范围。
// Java Program to illustrate the CopyOnWriteArrayList
// addAll(Collection c)
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
public class CopyOnWriteArrayListDemo {
public static void main(String args[])
{
// create an empty array list1 with initial
// capacity 5
CopyOnWriteArrayList list
= new CopyOnWriteArrayList();
// using add() method to add elements in the
// list
list.add(12);
list.add(20);
list.add(45);
// prints all the elements available in list1
System.out.println("Printing list1:");
for (Integer number : list)
System.out.println("Number = " + number);
// create an empty array list2 with an initial
// capacity
CopyOnWriteArrayList list2
= new CopyOnWriteArrayList();
// use add() method to add elements in list2
list2.add(25);
list2.add(30);
list2.add(31);
list2.add(35);
// prints all the elements available in list2
System.out.println("Printing list2:");
for (Integer number : list2)
System.out.println("Number = " + number);
// inserting all elements of list2 at third
// position
list.addAll(2, list2);
System.out.println("Printing all the elements");
// prints all the elements available in list1
for (Integer number : list)
System.out.println("Number = " + number);
}
}
输出:
Printing list1:
Number = 12
Number = 20
Number = 45
Printing list2:
Number = 25
Number = 30
Number = 31
Number = 35
Printing all the elements
Number = 12
Number = 20
Number = 25
Number = 30
Number = 31
Number = 35
Number = 45