OK, I’ve been google’ing the web, and I just can’t seem to find any solution to my problem. I found lots of solutions, just not any that fit.
I need to create an array of generics. But the generic type itself extends Comparable. When I try the following:
public class Hash<T extends Comparable<String>> {
private T[] hashTable;
private int tableSize;
Hash(int records, double load) {
tableSize = (int)(records / loadFactor);
tableSize = findNextPrime(tableSize);
hashTable = (T[])(new Object[tableSize]); //Error: Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
}
}
The problem is that the Object cannot be cast as a generic that extends Comparable. Is there a way around this?
Generics and arrays don’t mix, basically. The short answer is that you can work around this problem. The longer answer is that you probably shouldn’t and I’ll explain why.
You could use
Array.newInstance()like this:but you can’t create an array of your parameterized type.
Arrays are covariant. That means they retain the type of their elements at runtime. Java’s generics are not. They use type erasure to basically mask the implicit casting that is going on. It’s important to understand that.
So when you create an Object array you can’t cast it to, say, a Comparable array (or any other type) because that is not correct.
To give you an example. With generics this is perfectly legal:
It’s also why you can’t do this:
ie at runtime there is no knowledge of T’s class. This is why the above code is more often written as:
because their is no runtime type for the generic argument. But with arrays:
What you should be doing in this case (imho) is not using arrays but using an
ArrayList. In all honesty, there is very little reason to use arrays over anArrayListand generics is just one example of that.For a better and more complete explanation see the (excellent) Java Generics FAQ: