java.util.PriorityQueue.toArray() 方法
描述
toArray() 方法用于返回一个包含此队列中所有元素的数组。
声明
以下是 java.util.PriorityQueue.toArray() 方法的声明。
public Object[] toArray()
参数
NA
返回值
该方法调用返回一个包含此队列中所有元素的数组。
异常
NA
示例
下面的例子展示了 java.util.PriorityQueue.toArray() 的用法。
package com.tutorialspoint;
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String args[]) {
// create priority queue
PriorityQueue < Integer > prq = new PriorityQueue < Integer > ();
// insert values in the queue
prq.add(6);
prq.add(9);
prq.add(5);
prq.add(64);
prq.add(6);
System.out.println("Priority queue values are: "+ prq);
// get objects from the queue
Object[] arr = prq.toArray();
System.out.println("Value in array: ");
for ( int i = 0; i<arr.length; i++ ) {
System.out.println("Value: " + arr[i].toString()) ;
}
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Priority queue values are: [5, 6, 6, 64, 9] Value in array: Value: 5 Value: 6 Value: 6 Value: 64 Value: 9

