Java Utility Library

Java ArrayDeque - toArray() Method



The java.util.ArrayDeque.toArray() method returns an array containing all of the elements in the deque in proper sequence (from first to last element).

Syntax

public Object[] toArray()

Parameters

No parameter is required.

Return Value

Returns an array containing all elements of the deque.

Exception

NA

Example:

In the example below, the java.util.ArrayDeque.toArray() method returns an array containing all elements of the given deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);
    MyDeque.add(40);

    //convert the deque into an array
    Object[] Arr =  MyDeque.toArray();

    //print the array
    System.out.print("The Arr contains: ");
    for(int i = 0; i < Arr.length; i++)
      System.out.print(Arr[i]+ " ");
  }
}

The output of the above code will be:

The Arr contains: 10 20 30 40

❮ Java.util - ArrayDeque