当我试图将条目放入我的HashTable
时,我总是收到NullPointerException
。我不认为它是我的私有调整方法,而是与我的散列方法有关。以下三种方法。此外,当数组被实例化时,所有的值都被设置为null,并且它们的布尔可用性被设置为true。布尔可用性是为了查看添加下一个条目的位置(如果它们具有匹配的散列值),因为我们正在执行线性探测实现。继续在这个散列表推送方法中获得NullPointerExecption
public V put(K key , V value) {
V v = null;
int hashVal = hash(key);
size++;
if (size >= maxSize) {
resize();
} else {
while (!table[hashVal].isAvailable()) {
hashVal++;
}
table[hashVal]=newtable[hashVal] Entry<K= new Entry < K,V> V > (key, value);
table[hashVal].setAvailable(false);
return value;
}
return v;
}
private void resize() {
int _length = 2*length;2 * length;
maxSize = (int) MAX_LOAD_FACTOR * _length;
Entry<KEntry < K,V>[] V > [] old = table;
table=table = new Entry[_length];
size=0;size = 0;
for (int i=0;i<oldi = 0; i < old.length; i++) {
if (!old[i].isAvailable()) {
put(old[i].getKey(), old[i].getValue());
}
}
}
private int hash(Object o) {
return (o.hashCode() % length);
}
这里是我的入门级: 公共静态类条目{ 私人K键;私人V值为 ; 私有布尔可用;
public Entry(K key, V value) {
this.setKey(key);
this.setValue(value);
this.setAvailable(true);
}
public void setKey(K key) {
this.key = key;
}
public K getKey() {
return this.key;
}
public void setValue(V value) {
this.value = value;
}
public V getValue() {
return this.value;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
}
我在我的put方法的while循环中得到了NPE。
这里是我的初始化我的哈希表加几个本地变量
private int length, size;
private int maxSize;
/**
* The underlying array for this hashtable
*/
private Entry<K,V>[] table;
public HashTable() {this(11);}
@SuppressWarnings("unchecked")
public HashTable(int length) {
this.length=length;
table=new Entry[length];
for(int i=0;i<table.length;i++) {
table[i]=null;
}
maxSize=(int)(MAX_LOAD_FACTOR * length);
size=0;
}
你有堆栈吗? – 2013-02-20 04:58:48
请注意[家庭作业标签现已正式弃用](http://meta.stackexchange.com/q/147100/182862) – 2013-02-20 05:00:45
您在哪条线上获得例外? – 2013-02-20 05:02:41