【问题标题】:different type of error with arrayList and array of ObjectarrayList 和 Object 数组的不同类型的错误
【发布时间】:2018-12-17 12:20:25
【问题描述】:

我创建了一个ArrayList 和一个来自类StudentStudent 的对象数组不是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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      在第一种情况下,您会在线收到编译时错误

      java.util.Collections.sort(studentList);
      

      studentList 不满足方法sort 的参数定义。看sort的签名:

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

      sort 需要一个List&lt;T&gt;,其中泛型类型T 扩展了接口ComparableStudent 类无论如何都没有实现 Comparable。因此studentList 不能使用Collections.sort 排序。

      在另一个网站上,我们有 Arrays 方法

      public static void sort(Object[] a)
      

      这个方法只需要一个对象数组。数组student 适合,所以没有编译时错误。但在底层,这个方法也假设数组中的对象是Comparable。让我们看一下异常的堆栈跟踪:

      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)
      

      正如我们所见,countRunAndMakeAscendingStudent 实例转换为Comparable,但失败了。原因同上。

      至少有两种方法可以解决这个问题:

      a) 让Student 实现Comparable - 最好是这样:

        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;
          }
        }
      

      你必须根据你的需要实现方法compareTo(Student o)。这将适用于列表 studentList 和数组 student

      b) 使用java.util.Collections.sort(studentList, comparator)。这样Student 不需要自己实现Comparable。取而代之的是,您可以提供一个Comparator&lt;Student&gt; 来比较学生。此外,您可以使用具有相同studentList 的不同比较器。

      【讨论】:

        猜你喜欢
        • 2018-03-11
        • 2014-12-18
        • 2012-03-10
        • 2020-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多