这是我的班级:
public static class __9_7_Person implements Comparator<__9_7_Person> {
private int height;
private int weight;
public __9_7_Person(int height, int weight) {
this.height = height;
this.weight = weight;
}
public int compare(__9_7_Person p1, __9_7_Person p2) {
if (p1.height != p2.height) {
return p1.height - p2.height;
}
else {
return p1.weight - p2.weight;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个这样的数组:
__9_7_Person p[] = {new __9_7_Person(60, 100),
new __9_7_Person(70, 150),
new __9_7_Person(56, 90),
new __9_7_Person(75, 190),
new __9_7_Person(60, 95),
new __9_7_Person(68, 110),
};
Run Code Online (Sandbox Code Playgroud)
但是当我调用Arrays.sort(p)时出现异常:"线程中的异常"主"java.lang.ClassCastException:ch_9 $ __ 9_7_Person无法强制转换为java.lang.Comparable"
| 归档时间: |
|
| 查看次数: |
124 次 |
| 最近记录: |