日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java的Iterator源碼淺析

 一本正經(jīng)地胡鬧 2019-07-14

在java的集合中,List接口繼承Collection接口,AbstractList類實現(xiàn)了List接口,在AbstractList中的內(nèi)部類Itr實現(xiàn)了Iterator接口

ArrayList實現(xiàn)List接口并繼承AbstractList類,結(jié)構(gòu)圖如下:(圖片出自網(wǎng)絡(luò))

Iterator接口源碼:

復(fù)制代碼
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());
    }
}
復(fù)制代碼


AbstractList的內(nèi)部類Itr實現(xiàn)了Iterator接口,如下所示:

復(fù)制代碼
  private class Itr implements Iterator<E> {
        /**元素的下標(biāo)
         * Index of element to be returned by subsequent call to next.
         */
        int cursor = 0;

        /**上一個元素的下標(biāo)。如果元素已被刪除就設(shè)置為-1
         * 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;

        /**允許修改的次數(shù),違規(guī)操作會拋異常
         * The modCount value that the iterator believes that the backing
         * List should have.  If this expectation is violated, the iterator
         * has detected concurrent modification.
         */
        int expectedModCount = modCount;
       /*檢查是否還有下一個元素*/
        public boolean hasNext() {
            return cursor != size();
        }
      /*光標(biāo)下移,并且返回當(dāng)前的元素*/
        public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }
       /*移除元素*/
        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
復(fù)制代碼

ArrayList中的iterator()方法:

    public Iterator<E> iterator() {
        return new Itr();
    }

ArrayList中的內(nèi)部類Itr源碼功能類似于AbstractList的內(nèi)部類Itr。

閱讀了Iterator的源碼,再回頭看Iterator遍歷List的過程,理解就會深刻很多。

        List<String> list=new ArrayList<String>();
        list.add("apple"); list.add("banana"); list.add("watermelon");
        for (Iterator<String> iterator=list.iterator();iterator.hasNext();) {
            System.out.println( iterator.next());
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多