• Iterator接口实现类


    Iterator接口实现类


    1.AbstractList类内部类

    private class Itr implements Iterator<E> {
    

    Index of element to be returned by subsequent call to next.

    int cursor = 0;
    

    Index of element returned by most recent call to next or previous. Reset to -1 if this element is deleted by a call to remove.

    int lastRet = -1;
    

    The modCount value that the iterator believes that the backing List should have. If this expectation is violated, the iterator has detected concurrent modification.

    expectation:期望 violate:被违背 detected:检测到

    int expectedModCount = modCount;
    
    public boolean hasNext() {
        return cursor != size();
    }
    public E next() {
        // 并发修改检测
        checkForComodification();
        try {
            int i = cursor;
            E next = get(i);
            lastRet = i;//本次返回的元素的下标
            cursor = i + 1;//下一次调用next()方法返回的元素的下标。
            return next;
        } catch (IndexOutOfBoundsException e) {
            checkForComodification();
            throw new NoSuchElementException();
        } 
    }
    public void remove() {
        if (lastRet < 0)
            throw new IllegalStateException();//所以,调用remove()前最好先调用next()
        checkForComodification();
    
        try {
            AbstractList.this.remove(lastRet);
            if (lastRet < cursor)
                cursor--;//下一次调用next()方法返回的元素的下标。
            lastRet = -1;//所以,不能连续调用两次remove()方法
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException e) {
            throw new ConcurrentModificationException();
        }
    }
    
    final void checkForComodification() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException(); 
    }
    
    }
    

    上述内部类代码中使用到的外部类变量modCount:

    public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    

    The number of times this list has been structurally modified. Structural modifications are those that change the size of the list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.

    otherwise:除此以外 perturb:干扰 in such a fashion that:以这样的方式 yield:产生incorrect:不正确的

    This field is used by the iterator and list iterator implementation returned by the iterator and code listIterator methods. If the value of this field changes unexpectedly, the iterator (or list iterator) will throw a ConcurrentModificationException in response to the next, remove, previous, set or add operations. This provides fail-fast behavior, rather than non-deterministic behavior in the face of concurrent modification during iteration.

    unexpectedly:意外的 non-deterministic:不确定的

    Use of this field by subclasses is optional. If a subclass wishes to provide fail-fast iterators (and list iterators), then it merely has to increment this field in its add(int, E) and remove(int) methods (and any other methods that it overrides that result in structural modifications to the list). A single call to add(int, E) or remove(int) must add no more than one to this field, or the iterators (and list iterators) will throw bogus ConcurrentModificationExceptions. If an implementation does not wish to provide fail-fast iterators, this field may be ignored.

    merely:仅仅

    protected transient int modCount = 0;
    
    }
    
  • 相关阅读:
    dom 获取节点练习
    dom 节点的添加删除 与修改
    dom document属性练习
    ArrayList 去除重复元素
    ud类型聊天
    UDP数据发送
    指定标签所在div在新窗口打开
    日期Calendar
    Seraph介绍
    U盘cmd命令行修复
  • 原文地址:https://www.cnblogs.com/XiaoZhengYu/p/12871576.html
Copyright © 2020-2023  润新知