【发布时间】:2018-12-17 12:20:25
【问题描述】:
我创建了一个ArrayList 和一个来自类Student 和Student 的对象数组不是Comparable 接口的instanceof,所以当我使用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;
}}
我的问题是为什么数组列表会出现编译错误,为什么对象数组会出现运行时错误? 谢谢。
【问题讨论】:
标签: java arrays sorting collections