我正在尝试利用队列,但队列正在重新组织,我不知道为什么
使用队列的每个人似乎都工作得很好,似乎没有人遇到这个问题
public static void main(String[] args){
PriorityQueue<String> cue = new PriorityQueue<String>();
cue.offer("this");
cue.offer("that");
cue.offer("then");
System.out.printf("%s \n", cue);
System.out.printf("%s \n", cue.peek());
cue.poll();
System.out.printf("%s \n", cue);
}
Run Code Online (Sandbox Code Playgroud)
我希望它打印:
[this, that, then]
this
[that, then]
Run Code Online (Sandbox Code Playgroud)
但相反它打印:
[that, this, then]
that
[then, this]
Run Code Online (Sandbox Code Playgroud)
我不知道为什么
来自java doc PriorityQueue:
基于优先级堆的无界优先级队列.优先级队列的元素按其自然顺序排序
所以这是按设计的.
您可以使用a java.util.LinkedList(实现Queue接口并将像先出队列中的第一个一样),并为您提供预期的排序
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |