为了账号安全,请及时绑定邮箱和手机立即绑定

arrayList和Object数组的不同类型的错误

arrayList和Object数组的不同类型的错误

斯蒂芬大帝 2021-05-03 17:43:13
我ArrayList从类创建了一个对象数组,Student并且Student该类不是instanceofComparable接口,所以当我用ArrayList此代码编写代码时,会导致编译错误。import java.util.*;public class test {    public static void main(String[] args) {     ArrayList <Student> studentList = new ArrayList<>();     studentList.add(new Student("Ahmed",50));     studentList.add(new Student("Ameen",30));     java.util.Collections.sort(studentList);     System.out.println(studentList);}  }public class Student {    private String name;    private int score;    Student ( String name , int score){        this.name= name;        this.score = score;       }}当我用对象数组编写代码时,这会导致运行时错误import java.util.*;public class test {    public static void main(String[] args) {      Student [] student = new Student[2];      student[0]=new Student("Ahmed",50);      student[1]=new Student("Ameen",30);     java.util.Arrays.sort(student);     System.out.println(Arrays.toString(student));}}public class Student {    private String name;    private int score;    Student ( String name , int score){        this.name= name;        this.score = score;       }}我的问题是为什么我在数组列表中出现编译错误,为什么在对象数组中出现运行时错误?谢谢。
查看完整描述

2 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

ArrayList是泛型类,泛型的主要好处是可以在编译时而不是在运行时检测到错误。


查看完整回答
反对 回复 2021-05-19
  • 2 回答
  • 0 关注
  • 179 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信