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


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


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)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ArrayDeque addAll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。