這是代碼和輸出低於它爲什麼值「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