欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java Iterator源码

程序员文章站 2022-07-05 09:54:46
Iterator接口java的Iterator是个接口,其定义了四个方法.主要是hasNext,Next方法.public interface Iterator { boolean hasNext(); E next(); default void remove() { throw new UnsupportedOperationException("remove"); } default void forEachRem...

Iterator接口

java的Iterator是个接口,其定义了四个方法.主要是hasNext,Next方法.

public interface Iterator<E> {
  
    boolean hasNext();
    E next();

    default void remove() {
        throw new UnsupportedOperationException("remove");
    }

    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

Iterator接口的实现

比如在ArrayList中有个内部类Itr实现了Iterator接口,如下

 private class Itr implements Iterator<E> {
 		//下一个元素的索引位置
        int cursor;      
        //上一个元素的索引位置,初始值为-1
        int lastRet = -1;
        int expectedModCount = modCount;

        // prevent creating a synthetic constructor
        Itr() {}

        public boolean hasNext() {
        //szie为ArrayList的大小,因为Itr是内部类,所以可以访问ArrayList的size field
        //如果索引的数组不等于size的大小,就返回false,表明还有下一个
            return cursor != size;
        }


        public E next() {
            checkForComodification();
            int i = cursor;
            //如果索引的值大于ArrayList的元素数量,抛出异常
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            //如果索引大于数组容量,报并发异常,why?
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
                //游标往后移动一位,为后面做准备
            cursor = i + 1;
            //返回第i个元素
            return (E) elementData[lastRet = i];
        }

        public void remove() {
 
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
            //移除第i号元素
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i < size) {
                final Object[] es = elementData;
                if (i >= es.length)
                    throw new ConcurrentModificationException();
                for (; i < size && modCount == expectedModCount; i++)
                    action.accept(elementAt(es, i));
                // update once at end to reduce heap write traffic
                cursor = i;
                lastRet = i - 1;
                checkForComodification();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
    ```

本文地址:https://blog.csdn.net/u010711495/article/details/109576379

相关标签: java