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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。