这是代码和输出低于它为什么值“5”和“6”我的意思是如何在PriorityQueue中的轮询方法(类似于队列中的其他元素)之后设置新的优先级。我正准备参加java认证考试,由于这个概念我总是倾向于选择错误的答案,欢迎任何帮助。为什么在Java中的poll方法之后,PriorityQueue中的值会发生变化?
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[]) {
// create priority queue
PriorityQueue <Integer> prq = new PriorityQueue <Integer>();
// insert values in the queue
for (int i = 3; i < 10; 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: 3
Priority queue values after poll: [4, 6, 5, 9, 7, 8]
很难说了一些有趣的问题,而不会看到其他可能的答案。未指定打印元素的顺序。但是,您可以根据元素数量和显示的值得出结论。 – Thilo 2014-09-19 05:46:50