rke_poke 2017-05-12 14:03 采纳率: 50%
浏览 778
已采纳

数组排序是的类型转化问题

public class MaoPao2 {
@Test
public void test2() {
Student[] st = new Student[100];
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
String name = sc.nextLine();
int degree = Integer.parseInt(sc.nextLine());
Student s = new Student();
s.setName(name);
s.setDegree(degree);
st[i] = s;
}
Arrays.sort( st);
for (int i = 0; i < n; i++) {
System.out.println(st[i].getName() + ":" + st[i].getDegree());
}
}

}
package paixu_Code;

public class Student {
String name;
int degree;

public Student() {

}

public Student(String name, int degree) {
    super();
    this.name = name;
    this.degree = degree;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getDegree() {
    return degree;
}

public void setDegree(int degree) {
    this.degree = degree;
}

}
报的错是:
java.lang.ClassCastException: paixu_Code.Student cannot be cast to java.lang.Comparable
at paixu_Code.MaoPao2.test2(MaoPao2.java:64)
即:Arrays.sort( st);转化错误

  • 写回答

2条回答 默认 最新

  • 青鸟路过佳城 2017-05-12 14:31
    关注

    这个类型转换异常比较常见,原因可能是你输入了double类型,就不能转换为int,如果要转需要强转

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services