I create an ArrayList and an array of object from class Student and Student class isn't instanceof Comparable interface, so when I write my code with ArrayList this causes a compile error.
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;
}
}
and when I write my code with array of object this cause run time error
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;
}}
My question is why do I get a compile error with array list, and why do I get a run time error with array of object? Thanks.