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
因此,就基本数据类型而言,我已经做了一系列类型的联系语言(海关,而不是Java AP 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];
}
这留下了汇编时通用阵列生成错误。
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?