PriorityQueue class toArray(T[] a)method example
Returns an array containing all of the elements in this queue
PriorityQueue class toArray(T[] a)method example. This example shows you how to use toArray(T[] a) method.This method Returns an array containing all of the elements in this queue;
Here is the code:-
/**
* @Program that Returns an array containing all of the elements in this queue
* ToArray1.java
* Author:-RoseIndia Team
* Date:-28-May-2008
*/
import java.util.*;
public class ToArray1 {
public static void main(String[] args) {
//Creating a PriorityQueue
PriorityQueue p = new PriorityQueue();
//Adding element to the queue
p.add("1");
p.add("2");
p.add("3");
p.add("4");
p.add("5");
p.add("6");
//Returns an array containing all of the elements in this queue
Object[] y = p.toArray(new Object[0]);
System.out.println("Given array is: ");
//Displays the element of the queue
for (int i = 0; i < y.length; i++) {
System.out.print(y[i] + "\t");
}
}
} |
Output of the program:-
Given array is:
1 2 3 4 5 6 |
|