Java Deque 接口的 addAll() 方法用於將指定集合中的所有元素添加到 deque 的末尾。
指定者:
- 接口 Collection<E> 中的 addAll
用法:
boolean addAll(Collection< ? Extends E>c)
參數:
上述方法隻包含一個參數:
- 需要插入的元素 'c'。
返回:
如果給定的雙端隊列發生更改,則上述方法返回 true。
例子1
import java.util.ArrayDeque;
import java.util.Deque;
public class JavaDequeaddAllExample1 {
public static void main(String[] args) {
Deque<Integer> deque1 = new ArrayDeque<Integer>();
System.out.println("The marks for student1:");
deque1.add(28);
deque1.add(25);
deque1.add(26);
for (Integer integer:deque1) {
System.out.println(integer);
}
Deque<Integer> deque2 = new ArrayDeque<Integer>();
System.out.println("The marks for student2:");
deque2.add(22);
deque2.add(19);
deque2.add(20);
for (Integer integer:deque2) {
System.out.println(integer);
}
deque1.addAll(deque2);
System.out.println("The total list for the marks:");
for (Integer integer:deque1 ) {
System.out.println(integer);
}
}
}
輸出:
The marks for student1: 28 25 26 The marks for student2: 22 19 20 The total list for the marks: 28 25 26 22 19 20
例子2
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class JavaDequeaddAllExample2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Deque<String> deque = new ArrayDeque<String>();
System.out.println("Enter the name of Employees for List 1:");
for (int i = 0; i < 4; i++) {
String name = scanner.next();
deque.add(name);
}
Deque<String> deque1 = new ArrayDeque<String>();
System.out.println("Enter the name of Employees for List 2:");
for (int i = 0; i < 4; i++) {
String name = scanner.next();
deque1.add(name);
}
deque.addAll(deque1);
System.out.println("The final list for the employees is given as:");
for (String string:deque) {
System.out.println(string);
}
}
}
輸出:
Enter the name of Employees for List 1: Sam Ram Priya Urvashi Enter the name of Employees for List 2: Ravi Rita Yashika Lavish The final list for the employees is given as: Sam Ram Priya Urvashi Ravi Rita Yashika Lavish
相關用法
- Java Deque addLast()用法及代碼示例
- Java Deque add()用法及代碼示例
- Java Deque addFirst()用法及代碼示例
- Java Deque size()用法及代碼示例
- Java Deque pollLast()用法及代碼示例
- Java Deque remove()用法及代碼示例
- Java Deque removeLast()用法及代碼示例
- Java Deque poll()用法及代碼示例
- Java Deque pop()用法及代碼示例
- Java Deque pollFirst()用法及代碼示例
- Java Deque getLast()用法及代碼示例
- Java Deque getFirst()用法及代碼示例
- Java Deque iterator()用法及代碼示例
- Java Deque peek()用法及代碼示例
- Java Deque removeFirstOccurrence()用法及代碼示例
- Java Deque offerFirst()用法及代碼示例
- Java Deque offer()用法及代碼示例
- Java Deque element()用法及代碼示例
- Java Deque descendingIterator()用法及代碼示例
- Java Deque peekLast()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Deque addAll() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。