當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。