当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Deque addAll()用法及代码示例


Java Deque 接口的 addAll() 方法用于将指定集合中的所有元素添加到 deque 的末尾。

指定者:

  • 接口 Collection<E> 中的 addAll

用法:

boolean addAll(Collection< ? Extends E>c)

参数:

上述方法只包含一个参数:

  1. 需要插入的元素 '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 addAll() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。