我应该通过线性探测来实现我自己的哈希表,但是我在使用 remove 函数时遇到了问题。它无法正常工作,但我无法查明问题:
public void remove(Student s){
if(s==null) throw new NullPointerException("Student is null"); /*check if student is null*/
if (!contains(s)){ /*stop if students is not in table*/
throw new NullPointerException();
}
int i = hashFunction(s);
for(int j = 0; j<array.length; j++){
if(s.equals(array[j])){ /*found student*/
array[j]=null; /*delete student*/
break;
}
}
for(int k= i; k<array.length; k++){ /* find out if next element exists, if yes, move it to the gap of deleted student */
if(!(array[k]==null)){
int tempnext= hashFunction(array[k]);
if(i <= tempnext){
array[i]=array[tempnext];
}
}
}
}
我的调试器将此显示为输出,因此很明显我的方法存在表格中的空白问题:
Start:
Array-Indices: [ 0] [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7]
Hash-Values: [ 6] [ 1] [__] [ 6] [ 0] [ 5] [ 3] [ 0]
Number: [ 9] [ 6] [__] [ 2] [ 3] [ 4] [ 5] [ 8]
Removing s6
Array-Indices: [ 0] [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7]
Hash-Values: [ 6] [ 6] [__] [ 6] [ 0] [ 5] [ 3] [ 0]
Number: [ 9] [ 2] [__] [ 2] [ 3] [ 4] [ 5] [ 8]
Removing s5
Array-Indices: [ 0] [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7]
Hash-Values: [ 6] [ 6] [__] [ 5] [ 0] [ 5] [__] [ 0]
Number: [ 9] [ 2] [__] [ 4] [ 3] [ 4] [__] [ 8]
提前感谢您的帮助!