【发布时间】: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