A generic array is independent of any data type and whose type of information is evaluated at runtime. However, Java doesn't allow the array to be generic because, in Java, arrays contain information associated with their components, and this information at the runtime is used to allocate memory.
A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.
Java allows generic classes, methods, etc. that can be declared independent of types. However, Java does not allow the array to be generic. The reason for this is that in Java, arrays contain information related to their components and this information is used to allocate memory at runtime.
If generic array creation were legal, then compiler generated casts would correct the program at compile time but it can fail at runtime, which violates the core fundamental system of generic types.
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:
private Comparable[] hashtable;
...
hashtable = (Comparable[])Array.newInstance(Comparable.class, tableSize);
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:
List<String> list = new ArrayList<String>();
List<Integer> list2 = (List<Integer>)list;
list.add(3);
It's also why you can't do this:
public <T> T newInstance(T t) {
return new T(); // error!
}
ie at runtime there is no knowledge of T's class. This is why the above code is more often written as:
public <T> T newInstance(T t, Class<T> clazz) {
return clazz.newInstance();
}
because their is no runtime type for the generic argument. But with arrays:
String arr[] = new String[10];
Integer arr2[] = (Integer[])arr; // error!
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 an ArrayList
and generics is just one example of that.
For a better and more complete explanation see the (excellent) Java Generics FAQ:
Can I create an array whose component type is a concrete parameterized type?
No, because it is not type-safe.
Arrays are covariant, which means that an array of supertype references is a supertype of an array of subtype references. That is,
Object[]
is a supertype ofString[]
and a string array can be accessed through a reference variable of typeObject[]
....
The other answers here generally all advocate a better approach for this (especially the recommendation to use an ArrayList instead), but a simple answer in this specific case could be to do:
hashTable = (T[])(new Comparable[tableSize]);
(i.e. create an array of type raw Comparable instead of Object)
If you properly encapsulate all access to this array inside your Hash object this should work, but (as the other answers explain) you could leave yourself vulnerable.
The cast you're attempting
(T[])(new Object[tableSize]);
fails, because the items in the array are instances of Object. Object does not extend Comparable<String>
, so the cast (T[]) fails because T is defined as:
T extends Comparable<String>
To resolve this problem either:
Comparable<String>
hashTable
from an Array (which is not a generic type), to a generic collection type, e.g. List<T> hashTable = new ArrayList<T>(tableSize>)
You often run into problems when you need to instantiate something of a generic type. The easiest way to get around this is to pass the class of actually will be stored in on the constructor. This way you can construct from the actual type. Try something like this:
public class Hash<T extends Comparable<String>>
{
Hash(int records, double load, Class<T> class)
{
tableSize = (int)(records / loadFactor);
tableSize = findNextPrime(tableSize);
hashTable = java.lang.reflect.Array.newInstance(class, tableSize);
}
private T[] hashTable;
private int tableSize;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With