ArrayDeque toArray() Method in Java
The toArray() method of the java.util.ArrayDeque class allows you to generate an array with the same elements as the deque instance, i.e., if the specified deque can fit in the given array, it is returned. If not, a new array is created using the size of the specified deque and the runtime type of the given array. If the deque fits in the given array with enough space to spare, then the extra space is set to null. Practically, this method duplicates all of the elements in the deque and assembles them into a new array structure. The java.util.ArrayDeque.toArray() method returns an array containing all of the elements of deque in the appropriate order (from first to last), and the returned array has the same runtime type as the given array.
Syntax:
Object[] array = ArrayDeque.toArray()
Parameters: The method does not accept any parameters.
Return Value: The method returns an array containing the elements from the ArrayDeque.
Example 1:
The use of the Java ArrayDeque toArray() method with Strings is demonstrated in the example below. We are constructing an ArrayDeque of Strings, adding some elements into it, and then creating an array using the toArray() method. The array is iterated, and the elements in the array are printed in the output.
FileName: ArrayDequeToArrayExample.java
// importing util packages import java.util.*; public class ArrayDequeToArrayExample { // main method public static void main(String args[]) { // Creating an empty dequeArr DequedequeArr = new ArrayDeque (); // adding elements into the Deque using the add() method dequeArr.add("Welcome"); dequeArr.add("To"); dequeArr.add("Free"); dequeArr.add("Online"); dequeArr.add("Tutorials"); // Displaying the ArrayDeque System.out.println("The ArrayDeque is: " + dequeArr); // Creating an array named a and using toArray() Object[] a = dequeArr.toArray(); System.out.println("The array is:"); for (int j = 0; j < a.length; j++) System.out.println(a[j]); } }
Output:
The ArrayDeque is: [Welcome, To, Free, Online, Tutorials] The array is: Welcome To Free Online Tutorials
Example 2:
The use of the Java ArrayDeque toArray() method with integers is demonstrated in the example below. We are creating an ArrayDeque of Integer numbers, adding a few elements, and then constructing an array with the toArray() method. The array is iterated, and the elements are printed.
Filename: ArrayDequeDemo.java
// Java code to illustrate toArray() import java.util.*; public class ArrayDequeDemo { public static void main(String args[]) { // Creating an empty dequeArr DequedequeArr = new ArrayDeque (); // adding elements into the Deque using the add() method dequeArr.add(13); dequeArr.add(16); dequeArr.add(30); dequeArr.add(26); dequeArr.add(4); dequeArr.add(32); // Displaying the ArrayDeque System.out.println("The ArrayDeque is: " + dequeArr); // Creating an array named a and using toArray() Object[] a = dequeArr.toArray(); System.out.println("The array is:"); for (int j = 0; j < a.length; j++) System.out.println(a[j]); } }
Output:
The ArrayDeque is: [13, 16, 30, 26, 4, 32] The array is: 13 16 30 26 4 32