不确定我了解迭代器的实现

时间:2018-11-09 00:11:15

标签: java iterator

我应该为以下对象实现一个迭代器:

public class BinaryHeap<AnyType extends Comparable<? super AnyType>> extends AbstractQueue<AnyType>

Class,这些是其属性:

private static final int DEFAULT_CAPACITY = 100;
private int currentSize;
private AnyType [ ] array;
private boolean min;
private int modifications; //AKA "modcount"

我必须实现的两个方法(覆盖?)是hasNext() next();另外,我的迭代器必须是 fail-fast 。这是我的尝试:

 private class HeapIterator implements Iterator
{
    int expectedModCount = modifications;
    int currentPosition;

    public boolean hasNext()
    {
        return currentPosition < array.length;
    }


    public Object next() throws NoSuchElementException, 
                    ConcurrentModificationException, 
                    UnsupportedOperationException
    {

        if (modifications != expectedModCount)
            throw new ConcurrentModificationException();
        return array[currentPosition++];
    }

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

但是我向同学展示了它,他们说我的next()hasNext()函数虽然不好,但是没有时间解释原因。有帮助吗?

0 个答案:

没有答案
相关问题