ToArray() Method : ArrayDeque in Java
Declaration :
1 |
public Object[] toArray() |
Explanation :
Purpose | The java.util.ArrayDeque.toArray() method returns an array containing all of the elements in this deque in proper sequence. |
Parameters | NA |
Return Value | This method returns an array containing all of the elements in this deque. |
Exception | NA |
Java Program : Example
Below example will explain java.util.ArrayDeque.toArray() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.c4learn; import java.util.ArrayDeque; import java.util.Deque; public class ArrayDequeDemo { public static void main(String[] args) { // Create empty array deque with an initial capacity Deque<Integer> deque = new ArrayDeque<Integer>(8); // Use add() method to add elements deque.add(100); deque.add(200); deque.add(300); deque.add(400); deque.add(500); // Print all the elements available in deque for (Integer number : deque) { System.out.println("Element is " + number); } // Print size of the Array Object[] obj = deque.toArray(); System.out.println("Size of Array is : " + obj.length); } } |
Output of Program :
1 2 3 4 5 6 |
Element is 100 Element is 200 Element is 300 Element is 400 Element is 500 Size of Array is : 5 |