具有可比较接口的java通用列表

lf3rwulv  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(580)

这是我的密码。当我试图编译它时,我得到了一个运行时错误。它在第9行给了我一个错误(contact=(anytype[])new object[max_list];)

public class ArrayBasedSortedList<AnyType extends Comparable<AnyType>> 
{
     private AnyType[] contact;
     private static int MAX_LIST = 5;
     private int numItems;
     public ArrayBasedSortedList() 
     {
          contact = (AnyType[]) new Object[MAX_LIST];
     }

它给了我这个错误。

Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Comparable; ([Ljava.lang.Object; and [Ljava.lang.Comparable; are in module java.base of loader 'bootstrap')
    at ArrayBasedSortedList.<init>(ArrayBasedSortedList.java:9)
    at test.main(test.java:5)
alen0pnh

alen0pnh1#

@试着把它改成

contact = (ArrayType[]) new Comparable[MAX_LIST];.

相关问题