So I know normally to create a generic array you could do:
E[] e = (E[]) new Object[10];
However I have a class Entrant<K, V> which has two generic parameters.
I can't seem to be able to cast an Object array to it.
Here is the full code and error at runtime
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LHashTable.Entrant;
at HashTable.HashTable.<init>(HashTable.java:10)
at Mainy.map(Mainy.java:32)
line 32 in Mainy :
HashTable h = new HashTable();
Hashtable code:
public class HashTable<K, V> {
Entrant<K, V>[] _entrants;
private static final int N = 16;
public HashTable() {
_entrants = (Entrant<K, V>[]) new Object[N]; //line 10
}
}
Object[]is not anEntrant<K, V>[], so you get aClassCastException. See this question: stackoverflow.com/questions/1817524/generic-arrays-in-java?rq=1