如何在java中删除hashtable中的节点?

hsvhsicv  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(329)

我是个编程新手。我目前正在学习java编程语言。我的问题是,我试图删除一个特定的节点,其中包含一个哈希表中的值,但我不能找出我哪里出错了。有人能告诉我如何删除这个特定的节点吗?先谢谢你。对不起,我英语不好。
我想要瞄准的输出:“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();
    }
}
fbcarpbf

fbcarpbf1#

在快速测试之后,问题在于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。您没有更改中的实际hashentry数据 arrayHash .

相关问题