📜  Java中的集合 addAll() 方法和示例

📅  最后修改于: 2022-05-13 01:54:35.114000             🧑  作者: Mango

Java中的集合 addAll() 方法和示例

Java.util.Collections类的addAll()方法用于将所有指定元素添加到指定集合中。要添加的元素可以单独指定,也可以作为数组指定。此便捷方法的行为与 c.addAll(Arrays.asList(elements)) 的行为相同,但此方法在大多数实现下可能运行得更快。

句法:

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

参数:此方法将以下参数作为参数

  • c-要插入元素的集合
  • 元素- 要插入到 c 中的元素

返回值:如果集合因调用而更改,则此方法返回 true。

异常:如果元素包含一个或多个空值并且 c 不允许空元素,或者如果 c 或元素为空,则此方法抛出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
            List arrlist = new ArrayList();
  
            // 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
            List arrlist = new ArrayList();
  
            // 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