免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
java的Iterator源碼淺析

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

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

Iterator接口源碼:

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());    }}


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

  private class Itr implements Iterator<E> {        /**元素的下標(biāo)         * Index of element to be returned by subsequent call to next.         */        int cursor = 0;        /**上一個(gè)元素的下標(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ī)操作會(huì)拋異常         * 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;       /*檢查是否還有下一個(gè)元素*/        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();        }    }

ArrayList中的iterator()方法:

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

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

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

        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());
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java ConcurrentModificationException異常原因和解決方法
Java集合 iterator.remove()方法詳解
java提高篇(三十)
深入探討iterator模式
迭代器模式(Iterator pattern)
java.util.ConcurrentModificationException
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服