[英]Generic List with comparable interface
这是我的代码。 当我尝试编译它时出现运行时错误。 它在第 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)
@Slaw 尝试将其更改为
contact = (ArrayType[]) new Comparable[MAX_LIST];.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.