8

Possible Duplicate:
Java how to: Generic Array creation
Error generic array creation

I have been tasked with writing a Hash Table in Java, which must work with any data type. The rules on the code I am writing are as follows: - The hash table must have an array as the underlying data structures, of a size determined at the time the object is constructed - When a collision occurs, the element that collides should be placed into a linked list, which holds all of the elements at that index (key) in the hash table

Thus, for the underlying data type, I have made an array of type LinkedList (custom, not the Java API LinkedList).

private LinkedList<T>[] table;

The issue is, of course, instantiating this array. Here are some of my attempts:

public HashTable(int size) {
  table = new LinkedList<T>[size];
}

This throws a compile-time generic array creation error.

public HashTable(int size) {
  table = (LinkedList<T>[])(new Object[size]);
}

That causes a ClassCastException error at runtime (java.lang.Object cannot be cast to LinkedList).

The person heading the project is also unsure of how to deal with this issue. Is there any way I can change my code so that the hash table still has an array as its underlying data structure with the collisions being placed in a LinkedList?

Community
  • 1
  • 1
Tanaki
  • 2,233
  • 4
  • 27
  • 39

3 Answers3

6

This worked for me:

public class HashTable<T> {

    private LinkedList<T> table[];

    @SuppressWarnings("unchecked")
    public HashTable(int size) {
        table = new LinkedList[size];
    }

}

For example:

HashTable<String> t = new HashTable<String>(10);
t.table[0] = new LinkedList<String>();
t.table[0].add("test");
System.out.println(t.table[0].get(0));

Yes, the constructor generated a warning (that explains the "unchecked" annotation), but afterwards the code works without more warnings.

Óscar López
  • 215,818
  • 33
  • 288
  • 367
1

Just use Object[] as your data store, and manually cast it to the specific type. This is acceptable in building infrastructure stuff, where type relations can be harder than usual.

For what it's worth, this is the way to create generic array in Java:

@SafeVarargs
static <E> E[] newArray(int length, E... array)
{
    return Arrays.copyOf(array, length);
}

//used in your example

    private LinkedList<T>[] table;

    public HashTable(int size) {
        table = newArray(size);
    }
irreputable
  • 42,827
  • 9
  • 59
  • 89
0

It isn't ideal, but you can do this sort of thing:

import java.util.LinkedList;

public class Test
{
    static class HashTable<T>
    {
        public HashTable(int size)
        {
            LinkedList<T>[] table = (LinkedList<T>[])java.lang.reflect.Array.newInstance(LinkedList.class, size);
        }
    }

    public static void main(String[] args)
    {
        HashTable<Integer> table = new HashTable<Integer>(23);
    }
}
Stuart Golodetz
  • 19,132
  • 3
  • 45
  • 79
  • `Class` -> `Class` and don't trap and drop `Exception e`. And why not just pass `LinkedList.class` to `newInstance`? You don't need the `forName` lookup at all. – Mike Samuel Nov 08 '11 at 00:13
  • `Array.newInstance(LinkedList.class, size)` is exactly identical to `new LinkedList[size]` – user102008 Nov 19 '11 at 08:15