關(guān)于iterator

實現(xiàn)要明白iterator的好處都有撒?

舉個例子:

現(xiàn)在有一個 list 里邊兒有5個字符串 分別是 “a“ ”b” "c" "b" "a"

現(xiàn)在需要刪除list里的字符串 “b”

使用 for(int i = 0; i < list.size(); i++) 去做就比較麻煩

直接使用iterator就能輕松實現(xiàn)




以下為ArrayList中iterator的內(nèi)部類Itr及ListItr實現(xiàn)

/**

* Returns a list iterator over the elements in this list (in proper

* sequence), starting at the specified position in the list.

* The specified index indicates the first element that would be

* returned by an initial call to {@link ListIterator#next next}.

* An initial call to {@link ListIterator#previous previous} would

* return the element with the specified index minus one.

*

*

The returned list iterator is fail-fast.

*

* @throws IndexOutOfBoundsException {@inheritDoc}

*/

public ListIteratorlistIterator(int index) {

if (index <0 || index >size)

throw new IndexOutOfBoundsException("Index: "+index);

? ? return new ListItr(index);

}

/**

* Returns a list iterator over the elements in this list (in proper

* sequence).

*

*

The returned list iterator is fail-fast.

*

* @see #listIterator(int)

*/

public ListIteratorlistIterator() {

return new ListItr(0);

}

/**

* Returns an iterator over the elements in this list in proper sequence.

*

*

The returned iterator is fail-fast.

*

* @return an iterator over the elements in this list in proper sequence

*/

public Iteratoriterator() {

return new Itr();

}

/**

* An optimized version of AbstractList.Itr

*/

private class Itrimplements Iterator {

int cursor;? ? ? // index of next element to return

? ? int lastRet = -1; // index of last element returned; -1 if no such

? ? int expectedModCount =modCount;

? ? public boolean hasNext() {

return cursor !=size;

? ? }

@SuppressWarnings("unchecked")

public E next() {

checkForComodification();

? ? ? ? int i =cursor;

? ? ? ? if (i >=size)

throw new NoSuchElementException();

? ? ? ? Object[] elementData = ArrayList.this.elementData;

? ? ? ? if (i >= elementData.length)

throw new ConcurrentModificationException();

? ? ? ? cursor = i +1;

? ? ? ? return (E) elementData[lastRet = i];

? ? }

public void remove() {

if (lastRet <0)

throw new IllegalStateException();

? ? ? ? checkForComodification();

? ? ? ? try {

ArrayList.this.remove(lastRet);

? ? ? ? ? ? cursor =lastRet;

? ? ? ? ? ? lastRet = -1;

? ? ? ? ? ? expectedModCount =modCount;

? ? ? ? }catch (IndexOutOfBoundsException ex) {

throw new ConcurrentModificationException();

? ? ? ? }

}

@Override

? ? @SuppressWarnings("unchecked")

public void forEachRemaining(Consumer consumer) {

Objects.requireNonNull(consumer);

? ? ? ? final int size = ArrayList.this.size;

? ? ? ? int i =cursor;

? ? ? ? if (i >= size) {

return;

? ? ? ? }

final Object[] elementData = ArrayList.this.elementData;

? ? ? ? if (i >= elementData.length) {

throw new ConcurrentModificationException();

? ? ? ? }

while (i != size &&modCount ==expectedModCount) {

consumer.accept((E) elementData[i++]);

? ? ? ? }

// update once at end of iteration to reduce heap write traffic

? ? ? ? cursor = i;

? ? ? ? lastRet = i -1;

? ? ? ? checkForComodification();

? ? }

final void checkForComodification() {

if (modCount !=expectedModCount)

throw new ConcurrentModificationException();

? ? }

}

/**

* An optimized version of AbstractList.ListItr

*/

private class ListItrextends Itrimplements ListIterator {

ListItr(int index) {

super();

? ? ? ? cursor = index;

? ? }

public boolean hasPrevious() {

return cursor !=0;

? ? }

public int nextIndex() {

return cursor;

? ? }

public int previousIndex() {

return cursor -1;

? ? }

@SuppressWarnings("unchecked")

public E previous() {

checkForComodification();

? ? ? ? int i =cursor -1;

? ? ? ? if (i <0)

throw new NoSuchElementException();

? ? ? ? Object[] elementData = ArrayList.this.elementData;

? ? ? ? if (i >= elementData.length)

throw new ConcurrentModificationException();

? ? ? ? cursor = i;

? ? ? ? return (E) elementData[lastRet = i];

? ? }

public void set(E e) {

if (lastRet <0)

throw new IllegalStateException();

? ? ? ? checkForComodification();

? ? ? ? try {

ArrayList.this.set(lastRet, e);

? ? ? ? }catch (IndexOutOfBoundsException ex) {

throw new ConcurrentModificationException();

? ? ? ? }

}

public void add(E e) {

checkForComodification();

? ? ? ? try {

int i =cursor;

? ? ? ? ? ? ArrayList.this.add(i, e);

? ? ? ? ? ? cursor = i +1;

? ? ? ? ? ? lastRet = -1;

? ? ? ? ? ? expectedModCount =modCount;

? ? ? ? }catch (IndexOutOfBoundsException ex) {

throw new ConcurrentModificationException();

? ? ? ? }

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容