全屏
poll()方法用于检索并移除此队列的头,则返回null,如果此队列为空。
声明
以下是java.util.PriorityQueue.poll()方法的声明。public E poll()
参数NA
返回值该方法调用返回队列的头部,或null,如果队列为空。
异常NA
例子
下面的例子显示java.util.PriorityQueue.poll()方法的使用package cn.sxt;
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[]) {
// create priority queue
PriorityQueue prq = new PriorityQueue ();
// insert values in the queue
for ( int i = 3; i
prq.add (new Integer (i)) ;
}
System.out.println ( "Initial priority queue values are: "+ prq);
// get the head from the queue
Integer head = prq.poll();
System.out.println ( "Head of the queue is: "+ head);
System.out.println ( "Priority queue values after poll: "+ prq);
}
}
现在编译和运行上面的代码示例,将产生以下结果。Initial priority queue values are: [3, 4, 5, 6, 7, 8, 9]
Head of the queue is: 3Priority queue values after poll: [4, 6, 5, 9, 7, 8]
分享到:
0评论