泛型列表相媲美接口

问题描述 投票:-1回答:1

这是我的代码。我得到一个运行时错误,当我尝试编译它。它给我一个错误在第9行(接触=(ANYTYPE [])新的对象[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)
java
1个回答
0
投票

@Slaw尝试将其更改为

contact = (ArrayType[]) new Comparable[MAX_LIST];.
© www.soinside.com 2019 - 2024. All rights reserved.