如何遍历 PriorityQueue? [英] How to iterate over a PriorityQueue?

查看:115
本文介绍了如何遍历 PriorityQueue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for (Event e : pq)

不按优先级顺序迭代.

while(!pq.isEmpty()){
  Event e = pq.poll();
}

这有效,但会清空队列.

This works but empties the queue.

推荐答案

来自 Javadocs

方法 iterator() 中提供的迭代器不保证以任何特定顺序遍历 PriorityQueue 的元素.如果需要有序遍历,可以考虑使用Arrays.sort(pq.toArray()).

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

可能还有其他等效的机制.

There are probably other equivalent mechanisms.

这篇关于如何遍历 PriorityQueue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆