boolean addAll(Collection c)
此方法用於將集合c標識的所有元素追加到列表的末尾,順序與集合的迭代器返回的順序相同。
參數:此方法接受強製性參數c,該參數是包含要添加到列表末尾的元素的集合。
返回值:此方法返回一個布爾值。如果將集合正確插入列表中,則返回true;否則,返回false。
異常:如果指定的元素為null,則此方法引發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<Integer> list1
= new CopyOnWriteArrayList<Integer>();
// 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<Integer> list2
= new CopyOnWriteArrayList<Integer>();
// 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-如果指定的集合為null。
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<Integer> list
= new CopyOnWriteArrayList<Integer>();
// 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<Integer> list2
= new CopyOnWriteArrayList<Integer>();
// 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
相關用法
- Java Set addAll()用法及代碼示例
- Java SortedSet addAll()用法及代碼示例
- Java AbstractCollection addAll()用法及代碼示例
- Java AbstractSequentialList addAll()用法及代碼示例
- Java Collection addAll()用法及代碼示例
- Java List addAll()用法及代碼示例
- Java LinkedBlockingDeque addAll()用法及代碼示例
- Java AbstractList addAll()用法及代碼示例
- Java AbstractQueue addAll()用法及代碼示例
- Java Collections addAll()用法及代碼示例
- Java CopyOnWriteArraySet addAll()用法及代碼示例
- Java ConcurrentLinkedDeque addAll()用法及代碼示例
- Java CopyOnWriteArrayList set()用法及代碼示例
- Java CopyOnWriteArrayList addAllAbsent()用法及代碼示例
- Java CopyOnWriteArrayList retainAll()用法及代碼示例
注:本文由純淨天空篩選整理自saigopalCheela大神的英文原創作品 CopyOnWriteArrayList addAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。