Runtime error: Array of Lists with generics in Java

Runtime error: Array of Lists with generics in Java

我正在尝试执行以下操作以使用泛型实现单独的链接哈希 table:

// hash table class
protected List<HashEntry<K, V>>[] bucket;
this.bucket = (List<HashEntry<K, V>>[]) new Object[capacity];(*)
(...)
// main program
HashTableMapSC<Integer, String> hashSC = new HashTableMapSC<Integer,String>(7);

我收到以下 运行 时间错误:

[Ltablashash.HashTableMapSC$HashEntry; cannot be cast to [Ljava.util.List;

位于 (*)

HashEntry 只是一个简单的私有 class,它包含键值对。

我知道这行得通:

protected List<List<HashEntry<K,V>>> bucket;

但是因为有人要求我这样做,所以我想知道这是否可行。

谢谢

如果您确实需要泛型数组(通常不鼓励这样做),请使用

this.bucket = new List[capacity];

不过你会收到原始类型警告。