我编写了自己的 Hash 类,然后我用它来将 (String, LinkedList(String)) 对存储为 (K,V) 对。当我调用程序来填充哈希时,迭代器中会抛出一个 ArrayOutOfBoundsException。我尝试使数组比需要的大得多,但仍然遇到相同的错误。抛出错误的行是“nodes[j++] = n;”
abstract class IteratorHelper<E> implements Iterator<E> {
public DictionaryNode<K,V>[] nodes;
protected int index;
protected long modCheck;
public IteratorHelper() {
nodes = new DictionaryNode[currentSize];
index = 0;
int j = 0;
modCheck = modCounter;
System.out.println("int currentSize = " + currentSize);
System.out.println("int tableSize = " + tableSize);
for(int i=0; i<tableSize; i++)
for(DictionaryNode n : list[i]) {
System.out.println("int j = " + j);
System.out.println("DictionaryNode key at nodes[" + j + "] = " + n.key);
nodes[j++] = n;
}
nodes = (DictionaryNode<K,V>[]) Sorter.quickSort(nodes);
}
public boolean hasNext() {
if(modCheck != modCounter)
throw new ConcurrentModificationException();
return index < currentSize;
}
public abstract E next();
public void remove() {
throw new UnsupportedOperationException();
}
}
class KeyIteratorHelper<K> extends IteratorHelper<K> {
public KeyIteratorHelper() {
super();
}
public K next() {
return (K) nodes[index++].key;
}
}
class ValueIteratorHelper<V> extends IteratorHelper<V> {
public ValueIteratorHelper() {
super();
}
public V next() {
return (V) nodes[index++].value;
}
}
}