我正在做一项作业,我必须将 10,000 个数字散列到负载大小为 0.1、.2 .3 .... 最大为 0.9 的散列表中。我的问题是我的散列函数给了我一些溢出或类似的东西。如果我对负载因子为 0.5 的表(如 36077(mod)20,000)进行哈希处理,它会给我 16070 作为键。这只发生在高于负载因子的数字上。这是我的哈希函数的代码。
public int linearHash(int in){
int hashKey = in%hashTableArray.length;
while(this.hashTableArray[hashKey] != 0){
hashKey += 1;
}
return hashKey;
}
谢谢。
最佳答案
您没有检查 while
循环中是否超出了 hashTableArray
的索引范围。你可以这样做:
while (hashKey < hashTableArray.length && this.hashTableArray[hashKey] != 0){
关于Java:需要有关哈希函数溢出的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203164/