关于iterator

实现要明白iterator的好处都有撒?

举个例子:

现在有一个 list 里边儿有5个字符串 分别是 “a“ ”b” "c" "b" "a"

现在需要删除list里的字符串 “b”

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

直接使用iterator就能轻松实现




以下为ArrayList中iterator的内部类Itr及ListItr实现

/**

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

        }

}

}

你可能感兴趣的:(关于iterator)