Generic arrays in Java

Viewed 55003

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?

5 Answers
Related