使用Hash迭代器时抛出ArrayOutOfBoundsException

时间:2013-05-09 05:14:46

标签: java hash iterator

我编写了自己的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;
    }
}

}

0 个答案:

没有答案