gpt4 book ai didi

java - PriorityQueue(Java) : Why is ordering different than a sorted ArrayList? 的排序算法出现问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:30:55 26 4
gpt4 key购买 nike

<分区>

我正在研究霍夫曼。但是我发现PriorityQueue的排序算法有问题;它没有足够的比较!然后我就写了一个简单的类来测试Collections的排序和PriorityQueue的排序:

public class Student implements Comparable<Student>{

String name;
int score;
int math;

public Student(int score, int math, String name) {
this.name = name;
this.score = score;
this.math = math;
}

public int compareTo(Student other) {
if (this.score > other.score) {
return -1;
} else if (this.score < other.score) {
return 1;
} else {
if (this.math > other.math) {
return -1;
} else {
return 1;
}
}

return 0;
}

public String toString() {
return("[" + name + " has Score: " + score + "(Math: " + math + ")]");
}
}

但我得到了这样的结果(在控制台上):

Priority Queue::::::::::
[Jeremy Lin has Score: 2350(Math: 800)]
[Qian has Score: 2150(Math: 800)]
[PoorMath has Score: 2060(Math: 600)]
[Hui has Score: 1800(Math: 690)]
[Kaiyu has Score: 2060(Math: 800)]
[Chao has Score: 0(Math: 0)]

ArrayList sorted::::::::
[Jeremy Lin has Score: 2350(Math: 800)]
[Qian has Score: 2150(Math: 800)]
[Kaiyu has Score: 2060(Math: 800)]
[PoorMath has Score: 2060(Math: 600)]
[Hui has Score: 1800(Math: 690)]
[Chao has Score: 0(Math: 0)]

这个怎么解释?太奇怪了!

非常感谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com