[英]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.