ArrayDeque的addAll()方法用於在此ArrayDeque的末尾插入作為參數傳遞的集合的所有元素。要將集合的元素添加到ArrayDeque中,我們必須遍曆該集合,並使用addLast(E e)方法將每個元素添加到ArrayDeque中。此方法的用法方式與我們在此討論的相同,但工作量較小。如果此雙端隊列由於調用此方法而發生更改,則此方法返回True。
用法:
public boolean addAll(Collection<? extends E> c)
參數:此方法采用參數c,該參數表示我們要插入此雙端隊列的元素的Collection。
返回值:如果此雙端隊列由於調用此方法而發生更改,則此方法返回True。
異常:如果指定的集合或其任何元素為null,則此方法將引發NullPointerException。
以下示例程序旨在說明ArrayDeque的addAll()方法:
示例1:程序演示ArrayDeque上的addAll()方法,該方法將添加一個包含Numbers的集合。
// 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<Integer> Numbers = new ArrayDeque<Integer>();
// 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<Integer> col = new ArrayList<Integer>();
// 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<Integer> 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<String> students = new ArrayDeque<String>();
// 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<String> col = new LinkedList<String>();
// 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<String> 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<Integer> Numbers = new ArrayDeque<Integer>();
// 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<Integer> col = null;
try {
// call addAll() method
Numbers.addAll(col);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
java.lang.NullPointerException
參考: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#addAll(java.util.Collection)
相關用法
- Java Set addAll()用法及代碼示例
- Java NavigableSet addAll()用法及代碼示例
- Java ConcurrentLinkedQueue addAll()用法及代碼示例
- Java Vector addAll()用法及代碼示例
- Java LinkedList addAll()用法及代碼示例
- Java TreeSet addAll()用法及代碼示例
- Java Collection addAll()用法及代碼示例
- Java List addAll()用法及代碼示例
- Java AbstractQueue addAll()用法及代碼示例
- Java AbstractSequentialList addAll()用法及代碼示例
- Java ConcurrentLinkedDeque addAll()用法及代碼示例
- Java Stack addAll(int, Collection)用法及代碼示例
- Java Stack addAll(Collection)用法及代碼示例
- Java SortedSet addAll()用法及代碼示例
- Java CopyOnWriteArrayList addAll()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayDeque addAll() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。