I'm getting the error "Cannot create a generic array" for the following code:
public class MapImpl<K, V> {
private static int DEFAULT_CAPACITY = 16;
private int size;
// array holding the entries of the map
private Entry[] entries;
public MapImpl() {
entries = new Entry[DEFAULT_CAPACITY]; // error at this line: Cannot create a generic array of MapImpl<K,V>.Entry
}
// represents an entry in the map
private class Entry {
private K key;
private V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
Surprisingly, this works fine:
public class MapImpl<K, V> {
private static int DEFAULT_CAPACITY = 16;
private int size;
// array holding the entries of the map
private Entry<K, V>[] entries;
public MapImpl() {
entries = new Entry[DEFAULT_CAPACITY];
}
// represents an entry in the map
private class Entry<K, V> {
private K key;
private V value;
//but here K and V are being hidden.
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
}
I do understand that we can't create an array of generic type or a type that takes type parameters. But, here in my code the Entry class is not of a generic type. Am i overlooking something ?