【发布时间】:2018-06-03 23:19:15
【问题描述】:
假设我有一个优先队列
class Node{
public int id;
public int dist = Integer.MAX_VALUE;
public Node(int id) { this.id = id; }
}
Queue<Node> q = new PriorityQueue<>((a, b) -> Integer.compare(a.dist, b.dist));
如果我这样做了(假设一/两个是节点对象)
Node one = new Node(1);
Node two = new Node(2);
one.dist = 1;
two.dist = 2;
q.offer(one);
q.offer(two);
two.dist = -1;
队列的行为是什么? two会在现在之前出现吗?
谢谢!
【问题讨论】:
-
@BeeOnRope,我不知道它如何更完整......
-
为什么不直接运行代码并找出答案?
-
不,不会。队列不知道其中项目的属性或它们的变化。您需要删除、更新、插入。
-
您的代码会导致未定义的行为。文档明确指出您不能这样做。
-
@BoristheSpider 除非你能想出应该发生什么的规范,否则是的。
标签: java priority-queue