作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
我正在研究霍夫曼。但是我发现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)]
这个怎么解释?太奇怪了!
非常感谢!
我是一名优秀的程序员,十分优秀!