ハッシュ テーブルを使用してディクショナリを実装しようとしています (Java が提供するハッシュ テーブル クラスを使用するのではなく、ゼロから作成します)。以下はinsert()
、特定の配列位置に含まれるリンク リストに要素を挿入するために使用される Dictionary クラスのメソッドです。
Dictionary クラスが機能するかどうかを判断するために提供されたテスト プログラムを実行していますがArrayIndexOutOfBoundsException: -5980
、特定のポイントに達したときにエラーが発生します。以下に、特定のテストを示します。なぜこの例外が発生するのでしょうか? (必要に応じて、さらにコードを提供できます!)
入れる:
public int insert(DictEntry pair) throws DictionaryException {
String entryConfig = pair.getConfig();
int found = find(entryConfig);
if (found != -1) {
throw new DictionaryException("Pair already in dictionary.");
}
int entryPosition = hash(entryConfig);
if (dict[entryPosition] == null) { //Dictionary.java:54
LinkedList<DictEntry> list = new LinkedList<DictEntry>();
dict[entryPosition] = list;
list.add(pair);
return 0;
} else {
LinkedList<DictEntry> list = dict[entryPosition];
list.addLast(pair);
return 1;
}
}
テスト:
// Test 7: insert 10000 different values into the Dictionary
// NOTE: Dictionary is of size 9901
try {
for (int i = 0; i < 10000; ++i) {
s = (new Integer(i)).toString();
for (int j = 0; j < 5; ++j) s += s;
collisions += dict.insert(new DictEntry(s,i)); //TestDict.java:69
}
System.out.println(" Test 7 succeeded");
} catch (DictionaryException e) {
System.out.println("***Test 7 failed");
}
例外スタック トレース:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -5980
at Dictionary.insert(Dictionary.java:54)
at TestDict.main(TestDict.java:69)
ハッシュ関数:
private int hash(String config) {
int expBase = 41;
int exp;
int ASCIIChar;
int hashedConfig = 0;
for (int i = 0; i < config.length(); i++) {
ASCIIChar = (int)config.charAt(i);
exp = (int)Math.pow(expBase, i);
hashedConfig = hashedConfig + (ASCIIChar * exp);
}
hashedConfig = hashedConfig % dictSize;
return hashedConfig;
}