Java ArrayDeque toArray() Method



Description

The java.util.ArrayDeque.toArray() method returns an array containing all of the elements in this deque in proper sequence. The order in which elements are present in ArrayDeque is maintained in Array as well.

Declaration

Following is the declaration for java.util.ArrayDeque.toArray() method

public Object[] toArray()

Parameters

NA

Return Value

This method returns an array containing all of the elements in this deque.

Exception

NA

Getting Array from of an ArrayDeque of Integers Example

The following example shows the usage of Java ArrayDeque toArray() method with Integers. We're creating an ArrayDeque of Integers, adding some elements, and then array is created using toArray() method. The array is iterated and elements are printed.

package com.tutorialspoint;

import java.util.ArrayDeque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      ArrayDeque<Integer> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // get the array
      Object[] array = deque.toArray();

      for (Object object : array) {
         System.out.println(object);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

25
30
20
18

Getting Array from of an ArrayDeque of Strings Example

The following example shows the usage of Java ArrayDeque toArray() method with Strings. We're creating an ArrayDeque of Strings, adding some elements, and then array is created using toArray() method. The array is iterated and elements are printed.

package com.tutorialspoint;

import java.util.ArrayDeque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      ArrayDeque<String> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add("A");
      deque.add("B");
      deque.add("C");
      deque.add("D");        

      // get the array
      Object[] array = deque.toArray();

      for (Object object : array) {
         System.out.println(object);
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
B
C
D

Getting Array from of an ArrayDeque of Objects Example

The following example shows the usage of Java ArrayDeque toArray() method with Student objects. We're creating an ArrayDeque of Student objects, adding some elements, and then array is created using toArray() method. The array is iterated and elements are printed.

package com.tutorialspoint;

import java.util.ArrayDeque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      ArrayDeque<Student> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(new Student(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));       

      // get the array
      Object[] array = deque.toArray();

      for (Object object : array) {
         System.out.println(object);
      }
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

[ 1, Julie ]
[ 2, Robert ]
[ 3, Adam ]
java_util_arraydeque.htm
Advertisements