【问题标题】:key Iterator for chaining hash table java用于链接哈希表 java 的键迭代器
【发布时间】:2018-04-19 06:55:31
【问题描述】:

我正在为我的链式哈希表构建一个键和值迭代器的内部类。我遇到了麻烦。我首先检查了一个 HashTable 条目,然后迭代它指向的链表。就我对这个关键迭代器的跟踪而言,逻辑应该是正确的。这是它的样子:

private class KeyIterator implements Iterator<K> {

    private int currentIndex; // Current position in hash table
    //private int numberLeft; // Number of entries left in iteration

    private KeyIterator() {
        currentIndex = 0;
        //numberLeft = numberOfEntries;
    } // end default constructor

    public boolean hasNext() {
        return currentIndex < numberOfEntries;
    } // end hasNext

    public K next() {
        K result = null;

        if (hasNext()) {
            LList<TableEntry> items = hashTable[currentIndex];
            if (items != null) {
                Iterator<TableEntry> traverse = items.getIterator();
                while (traverse.hasNext()) {
                    TableEntry<K, V> item = traverse.next();
                    result = item.key;

//                        if (items.contains(item)) {
//                            hashTable[currentIndex] = items;
//                        }
//                        numberLeft--;
                    numberOfEntries++;
                }//reaches last entry in hash table w/ current index
                currentIndex++;
            } else {//items == null, that is, there is no llist in the current hash table
                currentIndex++;
            }
        } else {//reaches the end of the hash table
            throw new NoSuchElementException();
     }
        return result;
    } // end next

    public void remove() {
        throw new UnsupportedOperationException();
    } // end remove

} // end KeyIterator

但是,在我写的测试程序中,

import java.util.Iterator;
public class Demos {

public static void main(String[] args) {
    HashDict dictionary = new HashDict();

    dictionary.add("hello", 13);
    System.out.println("after first add: " + dictionary.toString());
    dictionary.displayTable();

    System.out.println("after second add: " + dictionary.add("hi", 12));
    dictionary.displayTable();

 }
}

我得到了这样的结果

after first add: 13
null, null
after second add: 12
null, null
null, null

displayTable 只是 HashTable 类中的一个方法,用于测试迭代器方法。它看起来像这样:

public void displayTable() {
    Iterator traverse = getKeyIterator();
    Iterator traverseValues = getValueIterator();

    while (traverse.hasNext()) {
        System.out.print(traverse.next());
        System.out.print(", " + traverseValues.next());
        System.out.println();
    }
}

为什么这个 keyIterator 方法会这样工作?任何帮助将不胜感激!

~~~~~~~~~~~~~

Key Iterator 的更新版本:

private class KeyIterator implements Iterator<K> {

    private int currentIndex; // Current position in hash table
    private int numberLeft; // Number of entries left in iteration

    private KeyIterator() {
        currentIndex = 0;
        numberLeft = numberOfEntries;
    } // end default constructor

    public boolean hasNext() {
        return numberLeft > 0;
    } // end hasNext

    public K next() {
        K result = null;
        if (hasNext()) {
            LList<TableEntry> items = hashTable[currentIndex];
            Iterator<TableEntry> traverse = items.getIterator();

            while (traverse.hasNext()) {
                TableEntry<K, V> item = new TableEntry();
                item = traverse.next();
                result = item.key;

                numberLeft--;
            }
            currentIndex++;
        } else {
            throw new NoSuchElementException();
        }
        return result;
    } // end next

    public void remove() {
        throw new UnsupportedOperationException();
    } // end remove
} // end KeyIterator

现在我的错误是这样的:

Exception in thread "main" java.lang.NullPointerException
at HashDict$KeyIterator.next(HashDict.java:48)
at Demos.main(Demos.java:40)

【问题讨论】:

    标签: java iterator key hashtable


    【解决方案1】:

    首先,你的键迭代器增加numberOfEntries 是没有意义的。由于该变量不是KeyIterator 类的实例变量,我假设它是封闭类的实例变量。只有在向哈希表添加或删除元素时才应修改它。

    另一个问题是currentIndex 不足以跟踪当前键的位置。您需要hashTable中的当前条目的索引+当前条目的链表中的当前链接。这将允许您在恒定时间内迭代到该链接列表中的下一个链接。

    【讨论】:

    • 这是有道理的。我误用 numberOfEntries 作为链表中当前链接的索引。我添加了另一个计数器来跟踪链接并对 next() 方法进行了一些更改,但仍然出现错误。我将更新新方法和我的问题中的错误。非常感谢!
    猜你喜欢
    • 2017-09-04
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多