📜  Java中的 ArrayDeque addAll() 方法

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

Java中的 ArrayDeque addAll() 方法

ArrayDeque 的addAll()方法用于在此 ArrayDeque 的末尾插入作为参数传递的集合的所有元素。为了将集合的元素添加到 ArrayDeque,我们必须遍历集合并使用 addLast(E e) 方法将每个元素添加到 ArrayDeque。此方法与我们在此处讨论的方法相同,但工作量较小。如果由于调用此方法而导致此双端队列发生更改,则此方法返回True

句法:

public boolean addAll(Collection c)

参数:此方法接受一个参数c ,它表示我们要插入此双端队列的元素的集合。

返回:如果由于调用此方法而导致此双端队列发生更改,则此方法返回True

异常:如果指定的集合或其任何元素为空,则此方法抛出NullPointerException

下面的程序说明了 ArrayDeque 的 addAll() 方法:
程序 1:演示 ArrayDeque 上的 addAll() 方法的程序,该方法将添加一个包含数字的集合。

// Java Program Demonstrate addAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque Numbers = new ArrayDeque();
  
        // Add Number to list
        Numbers.add(23);
        Numbers.add(32);
        Numbers.add(45);
        Numbers.add(63);
  
        // print ArrayDeque before calling addAll()
        System.out.println("Before calling addAll()");
        print(Numbers);
  
        // create a collection of Number to
        // add in ArrayDeque using addAll()
        ArrayList col = new ArrayList();
  
        // add Numbers in collection
        col.add(74);
        col.add(65);
        col.add(84);
  
        // add the elements of collection at the end
        // of ArrayDeque using addAll()
        Numbers.addAll(col);
  
        // print ArrayDeque
        System.out.println("After calling addAll()");
        print(Numbers);
    }
  
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque arDe)
    {
  
        arDe.forEach((n) -> System.out.print(n + " "));
  
        System.out.println();
    }
}
输出:
Before calling addAll()
23 32 45 63 
After calling addAll()
23 32 45 63 74 65 84

程序 2:演示 ArrayDeque 上的 addAll() 方法的程序,该方法将添加学生姓名的集合。

// Java Program Demonstrate addAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // create an ArrayDeque which going to
        // contains a list of Student names which is actually
        // string values
        ArrayDeque students = new ArrayDeque();
  
        // Add Strings to list
        // each string represents student name
        students.add("Ram");
        students.add("Mohan");
        students.add("Sohan");
        students.add("Rabi");
  
        // print ArrayDeque before calling addAll()
        System.out.println("Before calling addAll()");
        print(students);
  
        // create a collection of String to
        // add in ArrayDeque using addAll()
        LinkedList col = new LinkedList();
  
        // add Names in collection
        col.add("Rohan");
        col.add("Kartik");
  
        // add the elements of collection at the end
        // of ArrayDeque using addAll()
        students.addAll(col);
  
        // print ArrayDeque
        System.out.println("After calling addAll()");
        print(students);
    }
  
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque arDe)
    {
  
        System.out.println("List of Students Name:");
  
        arDe.forEach((n) -> System.out.print(" | " + n + " | "));
  
        System.out.println("\n");
    }
}
输出:
Before calling addAll()
List of Students Name:
 | Ram |  | Mohan |  | Sohan |  | Rabi | 

After calling addAll()
List of Students Name:
 | Ram |  | Mohan |  | Sohan |  | Rabi |  | Rohan |  | Kartik |

程序 3:演示 addAll() 方法抛出的异常的程序。

// Java Program Demonstrate Exception thrown by addAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque Numbers = new ArrayDeque();
  
        // Add Number to list
        Numbers.add(223);
        Numbers.add(132);
        Numbers.add(345);
        Numbers.add(563);
  
        // create a collection of Number which is null
        ArrayList col = null;
  
        try {
            // call addAll() method
            Numbers.addAll(col);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
java.lang.NullPointerException

参考: https: Java Java.util.Collection)