Java Collection Tutorial - Java PriorityQueue.toArray(T[] a)








Syntax

PriorityQueue.toArray(T[] a) has the following syntax.

public <T> T[] toArray(T[] a)

Example

In the following code shows how to use PriorityQueue.toArray(T[] a) method.

import java.util.PriorityQueue;
/*from w  w  w . j av  a2s  . com*/
public class Main {
   public static void main(String args[]) {

      PriorityQueue<Integer>   prq = new PriorityQueue<Integer>(); 
       
      // insert values in the queue
      prq.add(1);  
      prq.add(2);
      prq.add(1);
      prq.add(64);
      prq.add(1);
      
      System.out.println(prq);
      
      // create arr1
      Integer[] arr1 = new Integer[5];
      
      // use toArrsy() method
      Integer[] arr2 = prq.toArray(arr1); 
      
      for ( int i = 0; i<arr1.length; i++ ){  
         System.out.println (arr1[i]) ; 
      }
      
      for ( int i = 0; i<arr2.length; i++ ){  
         System.out.println (arr2[i]) ; 
      }
   }
}

The code above generates the following result.