我正在尝试实现通用的HashTable。在我的代码中,数组中有节点。我正在尝试使用通用Node类在默认构造函数中初始化数组的大小。我正在尝试在没有@SuppressWarnings
我已经尝试过table = (Node<K,V>[]) new Object[capacity];
,但收到相同的警告:
这就是我所拥有的:
public class HashTable<K,V> implements Table<K,V>{
private static final int INIT_CAPACITY = 4;
private int n;
public int tableSize;
public Node<K,V>[] table;
public HashTable(){
this(INIT_CAPACITY);
}
public HashTable(int capacity){
this.tableSize = capacity;
this.n = 0;
//table = (Node<K,V>[]) new Object[capacity]; // Also gives warnings
table = (Node<K,V>[]) new Node[capacity]; // Warnings occur here
}
和我的通用节点类: 公共类Node {
public K nodeKey; // key
public V nodeValue; // value
Node<K,V> next;
public Node(){
}
public Node(K key, V value, Node<K,V> next){
this.nodeKey = key;
this.nodeValue = value;
this.next = next;
}
public K getKey(){
return nodeKey;
}
public V getValue(){
return nodeValue;
}
}
谢谢! 这是我编译时得到的:
HashTable.java:17: warning: [rawtypes] found raw type: Node
table = (Node<K,V>[]) new Node[capacity];
^
missing type arguments for generic class Node<K,V>
where K,V are type-variables:
K extends Object declared in class Node
V extends Object declared in class Node
HashTable.java:17: warning: [unchecked] unchecked cast
table = (Node<K,V>[]) new Node[capacity];
^
required: Node<K,V>[]
found: Node[]
where K,V are type-variables:
K extends Object declared in class HashTable
V extends Object declared in class HashTable
2 warnings