1

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.

2 Answers 2

2

ArrayList is generic Class and the key benefit of generics is to enable errors to be detected at compile time rather than at runtime.

Sign up to request clarification or add additional context in comments.

Comments

1

In the first case you get a compile time error on line

java.util.Collections.sort(studentList);

studentList does not satisfy the parameter definition of the method sort. Look at the signature of sort:

public static <T extends Comparable<? super T>> void sort(List<T> list) 

sort expects a List<T> where the generic type T is extending the interface Comparable. The class Student doesn't implement Comparable in anyway. Thus studentList cannot be sorted using Collections.sort.

On the other site we have the Arrays method

public static void sort(Object[] a)

This method doesn't require more than an object array. The array student fits, so no compile time error. But under the hood also this method assumes that the objects in the array are kind of Comparable. Let'a have a look at the exception's stacktrace:

Exception in thread "main" java.lang.ClassCastException: playground.Example$Student cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:316)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:184)
    at java.util.Arrays.sort(Arrays.java:1244)
    at playground.Example.main(Example.java:20)

As we see countRunAndMakeAscendingcasts a Student instance to Comparable and this fails. The cause is the same as above.

There at least two ways to solve this:

a) Let Student implement Comparable - preferably like this:

  public class Student implements Comparable<Student> {
    private String name;
    private int score;

    Student(String name, int score) {
      this.name = name;
      this.score = score;
    }

    @Override
    public int compareTo(Student o) {
      // implement comparision by name and score for exmaple
      return 0;
    }
  }

You have to implement the method compareTo(Student o) according to your needs. This will work for the list studentList and for the array student.

b) Use java.util.Collections.sort(studentList, comparator). This way Student doesn't need to implement Comparable by it's own. Instead of this you can provide a Comparator<Student> which will compare the students. Moreover you can use different comparators with the same studentList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.