當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Collections addAll()用法及代碼示例


java.util.Collections類的addAll()方法用於將所有指定的元素添加到指定的集合中。要添加的元素可以單獨指定或作為數組指定。此便捷方法的行為與c.addAll(Arrays.asList(elements))的行為相同,但是在大多數實現下,此方法的運行速度可能明顯更快。

用法:

public static  boolean 
    addAll(Collection c, T... elements)

參數:此方法將以下參數作為參數


  • c-要插入元素的集合
  • elements-要插入c的元素

返回值:如果集合由於調用而發生更改,則此方法返回true。

異常:如果元素包含一個或多個null值並且c不允許為null元素,或者c或元素為null,則此方法引發NullPointerException

以下示例說明了addAll()方法

示例1:

// Java program to demonstrate 
// addAll() method 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // creating object of List<String> 
            List<String> arrlist = new ArrayList<String>(); 
  
            // Adding element to arrlist 
            arrlist.add("A"); 
            arrlist.add("B"); 
            arrlist.add("C"); 
            arrlist.add("Tajmahal"); 
  
            // printing the arrlist before operation 
            System.out.println("arrlist before operation : " + arrlist); 
  
            // add the specified element to specified Collections 
            // using addAll() method 
            boolean b = Collections.addAll(arrlist, "1", "2", "3"); 
  
            // printing the arrlist after operation 
            System.out.println("result : " + b); 
  
            // printing the arrlist after operation 
            System.out.println("arrlist after operation : " + arrlist); 
        } 
  
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
arrlist before operation : [A, B, C, Tajmahal]
result : true
arrlist after operation : [A, B, C, Tajmahal, 1, 2, 3]

輸出:

arrlist before operation : [A, B, C, Tajmahal]
result : true
arrlist after operation : [A, B, C, Tajmahal, 1, 2, 3]

示例2:對於NullPointerException

// Java program to demonstrate 
// addAll() method 
  
import java.util.*; 
  
public class GFG1 { 
    public static void main(String[] argv) throws Exception 
    { 
        try { 
  
            // creating object of List<String> 
            List<String> arrlist = new ArrayList<String>(); 
  
            // Adding element to arrlist 
            arrlist.add("A"); 
            arrlist.add("B"); 
            arrlist.add("C"); 
            arrlist.add("Tajmahal"); 
  
            // printing the arrlist before operation 
            System.out.println("arrlist before operation : " + arrlist); 
  
            // add the specified element to specified Collections 
            // using addAll() method 
            System.out.println("\nTrying to add the null value with arrlist"); 
            boolean b = Collections.addAll(null, arrlist); 
  
            // printing the arrlist after operation 
            System.out.println("result : " + b); 
  
            // printing the arrlist after operation 
            System.out.println("arrlist after operation : " + arrlist); 
        } 
  
        catch (NullPointerException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
arrlist before operation : [A, B, C, Tajmahal]

Trying to add the null value with arrlist
Exception thrown : java.lang.NullPointerException


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Collections addAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。