我正在尝试在实现Comparable接口的类上实现PriorityQueue
。>
当我运行代码时,它将以正确的输出产生。
testA测试CtestB
但是据我所知输出应该是:
testA测试BtestC
public class Test { public static void main(String[] args) { A testA = new A("testA", 0); A testB = new A("testB", 1); A testC = new A("testC", 2); PriorityQueue<A> q = new PriorityQueue<A>(); q.add(testA); q.add(testB); q.add(testC); testA.value = 600; testB.value = 700; testC.value = 800; System.out.println(q.poll()); System.out.println(q.poll()); System.out.println(q.poll()); } private static class A implements Comparable<A> { private final String name; private int value; private A(String name, int value) { this.name = name; this.value = value; } public int compareTo(A o) { Integer thisValue = this.value; Integer otherValue = o.value; return thisValue.compareTo(otherValue); } public String toString() { return name; } } }
[请帮助我了解我在哪里弄错了以及如何更正此代码。
我正在尝试在实现Comparable接口的类上实现PriorityQueue。当我运行代码时,它会产生正确的输出。 testA testC testB但是据我了解,输出...
我得到的输出为