问题描述
我是编程新手。我目前正在学习 Java 编程语言。我的问题是,我试图删除一个包含 HashTable 值的特定节点,但我无法弄清楚我哪里出错了。有人可以向我解释如何删除这个特定节点吗?先感谢您。抱歉我的英语不好。
我想要瞄准的输出:“Albert” 和 “Timmy” 应该被删除。
这是我的代码:
类:HashEntry
public class HashEntry {
private int key;
private String value;
private HashEntry next;
public HashEntry() {
this.next = null;
}
public HashEntry(int key,String value) {
this.key = key;
this.value = value;
this.next = null;
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
public void setNext(HashEntry next) {
this.next = next;
}
public HashEntry getNext() {
return this.next;
}
}
类:HashTableArray
public class HashTableArray {
HashEntry[] arrayHash;
int size;
public HashTableArray(int size) {
this.size = size;
arrayHash = new HashEntry[size];
}
public int getHash(int key) {
return key % size;
}
public void insert(int key,String value){
int hashInd = getHash(key);
HashEntry newVal = new HashEntry(key,value);
if (arrayHash[hashInd] == null) {
arrayHash[hashInd] = newVal;
}
else {
HashEntry arrayValue = arrayHash[hashInd];
while (arrayValue.getNext() != null) {
arrayValue = arrayValue.getNext();
}
arrayValue.setNext(newVal);
}
}
public void displayTable() {
System.out.println("Hash Table:");
for (int i=0; i<arrayHash.length; i++) {
if(arrayHash[i] != null) {
HashEntry temp = arrayHash[i];
while(temp.getNext() != null) {
System.out.println(temp.getKey() + "," + temp.getValue());
temp = temp.getNext();
}
System.out.println(temp.getKey() + "," + temp.getValue());
}
}
}
public void delete (int key,String value) {
int hashInd = getHash(key);
HashEntry head = arrayHash[hashInd];
if (arrayHash[hashInd] != null) {
HashEntry temp = head,prev = null;
if (temp.getNext() == null && head.getValue().equalsIgnoreCase(value)) {
head = null;
}
else if (temp.getNext() != null && temp.getValue().equalsIgnoreCase(value)) {
head = temp.getNext();
}
else {
while(!temp.getValue().equalsIgnoreCase(value)) {
prev = temp;
temp = temp.getNext();
}
if (temp == null) {
prev.setNext(null);
}
else {
prev.setNext(temp.getNext());
}
}
}
}
}
类:HashTableProgram
public class HashTableProgram {
public static void main(String[] args) {
HashTableArray h = new HashTableArray (10);
h.insert(12,"Albert");
h.insert(26,"Johnson");
h.insert(5,"Timmy");
h.insert(12,"George");
h.displayTable();
h.delete(12,"Albert");
h.delete(5,"Timmy");
System.out.println("\nAfter deleted...");
h.displayTable();
}
}
解决方法
快速测试后,问题出在你HashTableArray中的delete方法,你应该改一下
head = null;
head = temp.getNext();
到
arrayHash[hashInd] = null;
arrayHash[hashInd] = temp.getNext();
最终代码如下:
public void delete(int key,String value) {
int hashInd = getHash(key);
HashEntry head = arrayHash[hashInd];
if (arrayHash[hashInd] != null) {
HashEntry temp = head,prev = null;
if (temp.getNext() == null && head.getValue().equalsIgnoreCase(value)) {
arrayHash[hashInd] = null;
} else if (head.getNext() != null && head.getValue().equalsIgnoreCase(value)) {
arrayHash[hashInd] = temp.getNext();
} else {
while (!temp.getValue().equalsIgnoreCase(value)) {
prev = temp;
temp = temp.getNext();
}
if (temp == null) {
prev.setNext(null);
} else {
prev.setNext(temp.getNext());
}
}
}
}
问题在于,当您像这样将 arrayHash[hashInd]
分配给 head
时:
HashEntry head = arrayHash[hashInd];
您正在创建一个引用(或指针)变量 head
,该变量指向堆上的实际 HashEntry 数据。如果您尝试将 head
设置为 null,您只是在更改 head
所指向的内容,现在它指向 null。您没有更改 arrayHash
中的实际 HashEntry 数据。