問題已解決(目前無法接受答案,我改變了我的while循環,現在它可以工作,我在下面爲任何未來的觀衆回答了這個問題):我的哈希算法algortihm讓我頭痛。我從鑰匙的文件讀取輸入,並使用少量的鑰匙,我的代碼工作,一旦我使它成爲300字,我有問題。哈希函數位於底部,while循環位於主函數的主體中,用java編寫。哈希函數運行良好,身體也一樣,直到我愚蠢地改變了身體並丟失了原始代碼。我想我涵蓋了溢出問題,但任何幫助將不勝感激,謝謝!讓我的哈希算法工作出現問題
t大小是如何計算的:
//Calcking tSize
tSize = (int)(items*tSizeFactor);
//Making tSize prime
while(!isPrime((int)tSize))
tSize++;
While循環,當我從文件中讀取:
while(line != null) {
//Getting the address to place the value in
position = hash(line.toCharArray(), (int)tSize);
//If there is something there we enter the if statement
if(hashTable[position][0] != null) {
//while we haven't found a spot and i < tableSize we update the last position we were at and move through the array
for(int i = 1; i < (int)tSize && hashTable[position][0] != null; i++) {
//prevPosition is used to update the link in the spot just before our final destination, allows wrap around in the array
prevPosition = position;
//we add +i to the original position and modulo the table size allowing wrap around in the array
position = (position+i)%(int)tSize;
}
//finally when we found a spot we update the previous position to link to the new item
hashTable[prevPosition][1] = Integer.toString(position);
}
//Adding the values to the hash table and setting the link to -1
hashTable[position][0] = new String(line);
hashTable[position][1] = new String(Integer.toString(-1));
line = reader.readLine();
}
public static int hash(char ch[],final int TSIZE) {
int sum = 7;
for(int i = 0; i < ch.length; i++) {
sum = sum*31+ch[i];
sum <<= 3;
}
if(sum < 0)
sum *= -1;
return sum%TSIZE;
}
我不知道300是一個測試用例* *龐大的數字。 – 2013-04-09 22:51:37
除非你的文件中充滿了諸如「Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygon」這樣的詞,300並不是那麼多。 – ApproachingDarknessFish 2013-04-09 22:54:34
「有問題」有什麼問題? – leonbloy 2013-04-09 22:56:10