当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java CopyOnWriteArrayList addAll()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自saigopalCheela大神的英文原创作品 CopyOnWriteArrayList addAll() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。