[英]Java delete element from hashmap
我有HashMap,我正在嘗試根據以下算法從中刪除元素:
來源: https : //en.wikipedia.org/wiki/Linear_probing#Deletion
這是我目前擁有的,但是查看我的測試,實際上並沒有刪除該實例。 您能幫我弄清楚為什么嗎? 提前致謝
hashFunction()的結果是計算得出的哈希值。
public class MyHashMap {
Person[] array;
public void remove(Person p) {
int i = hashFunction(p);
while (!p.equals(array[i])) {
i = hashFunction(array[++i]);
}
int b;
while (array[i] != null) {
b = hashFunction(array[++i]);
if (b <= hashFunction(array[i])) {
Person newHash = array[i];
array[i] = null;
array[i++] = newHash;
remove(array[i++]);
}
else if (array[++i] == null) {
array[i] = null;
}
}
}
您的增量后和增量前運算符似乎有助於解決您的問題。 例如,
array[i] = null;
array[i++] = newHash;
在這里,您要為array[i]
分配null
,然后在i
遞增之前再次將newHash
分配給array[i]
。 您實際上可能想使用++i
而不是i++
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.