0

How can I instantiate an array of object of a Generic Class?

I was implementing a Hash Table in Java.

The Generic Class to be instantiated:

class GenericLinkedList<T> {
   // Generic Class Codes Here
}

Hash Table Class:

public class HashTable {

    private GenericLinkedList[] table;     // Generic Class Instantiation
    private static final int SIZE = 50;

    public HashTable() {
        this.table = new GenericLinkedList[SIZE];
        for(int i = 0; i < SIZE; i++)
            this.table[i] = new GenericLinkedList();
    }
}
apxcode
  • 7,367
  • 7
  • 25
  • 39
Beyond Noob
  • 115
  • 10

3 Answers3

2

You can't create an array of generic type. The following code is invalid:

List<String>[] listArray = new List<String>[10];  // Error

It would be better to use an Object[] internally to store the elements, and let the method returning the elements do appropriate cast:

public class HashTable<T> {

    private Object[] table;  
    private static final int SIZE = 50;

    public HashTable(){
        this.table = new Object[SIZE];
        for(int i = 0; i < SIZE; i++)
            this.table[i] = new GenericLinkedList();
    }
}

FYI, this is how the java.util.ArrayList is implemented.

P.S.: Why your Hashtable doesn't seem to have key-value mapping kind of thing? This is more like a list.

Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
  • and `GenericLinkedList[]` would be even better – newacct May 01 '14 at 07:23
  • Your sample isn't an array of a generic type but of a parameterized type. An array of a parameterized type should be perfectly legal. See also http://stackoverflow.com/q/529085/1686330 – Dirk Lachowski May 01 '14 at 08:18
  • @DirkLachowski You understand that OP is not creating an array of `T` type parameter, but of another generic class. And yes I did meant array of parameterized type, and since when did it became legal? Try `new ArrayList[10];`, and come back to me. The link you posted is working with `T[]` and it doesn't really create an array of `T` at compile time, but uses `Class` information to create array at runtime. How do you get an `ArrayList.class`? You can't. See for more details [my answer to this question](http://stackoverflow.com/q/18581002/1679863) – Rohit Jain May 02 '14 at 04:34
  • 1
    @RohitJain My bad. Your linked answer is amazing. Thanks for the lesson. – Dirk Lachowski May 02 '14 at 07:32
0

First of all, there is nothing wrong with the code you posted. You probably wanted table to be GenericLinkedList<T>[] though. The solution is simple, when creating the array, use either new GenericLinkedList[SIZE] or new GenericLinkedList<?>[SIZE].

public class HashTable<T> {

    private GenericLinkedList<T>[] table;  
    private static final int SIZE = 50;

    public HashTable(){
        this.table = new GenericLinkedList[SIZE];
        for(int i = 0; i < SIZE; i++)
            this.table[i] = new GenericLinkedList();
    }
}

or

public class HashTable<T> {

    private GenericLinkedList<T>[] table;  
    private static final int SIZE = 50;

    public HashTable(){
        this.table = (GenericLinkedList<T>[])new GenericLinkedList<?>[SIZE];
        for(int i = 0; i < SIZE; i++)
            this.table[i] = new GenericLinkedList();
    }
}
newacct
  • 110,405
  • 27
  • 152
  • 217
-1

Why is not your HashTable generic itself? HashTable<T> nicely solves your problem:

this.table = new GenericLinkedList<T>[SIZE];

You might also use GenericLinkedList<?>.

cadrian
  • 7,095
  • 2
  • 30
  • 41