NullPointerException
にエントリを入れようとすると、取得し続けHashTable
ます。私はそれが私のプライベートなサイズ変更方法ではなく、私のハッシュ方法と関係があると思います。方法は以下の3つです。また、配列がインスタンス化されると、すべての値が null に設定され、ブール値の可用性が true に設定されます。ブール値の可用性は、一致するハッシュがある場合に次のエントリを追加する場所を確認することです。これは、線形プローブの実装を行っているためです。
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);
}
これが私のエントリ クラスです。 public static class Entry { private K key; プライベート 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;
}