Java中使用size()方法遍歷PriorityQueue出現元素不全的問題
阿新 • • 發佈:2019-01-10
在使用PriorityQueue類時
使用
for(int i=0; i<priorityqueue.size(); i++)
與
for(int i : priorityqueue)
遍歷PriorityQueue時
有時會出現PriorityQueue未能正常排序
甚至是size()方法未能正確返回PriorityQueue的大小的問題
具體為什麼會出現這個問題我也不是很清楚
但是如果我們能用
while(!priorityqueue.isEmpty()){ priorityqueue.poll() }就不會出現這種問題
示例程式碼:
int temp2 = -1; for(int i=0; i<m; i++) { if(temp2 != people3.peek()) { temp2 =people3.peek(); }else { people3.poll(); continue; } for(Person person2 : persons) { if(person2.score == people3.peek()) { int w = person2.id.size(); while(!person2.id.isEmpty()) { System.out.println(person2.id.poll() + " " + person2.score); } people3.poll(); break; } } }