Created
July 10, 2013 14:50
-
-
Save lichenbo/5966950 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class Itr implements Iterator<E> { | |
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(); | |
} | |
} | |
final void checkForComodification() { | |
if (modCount != expectedModCount) | |
throw new ConcurrentModificationException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment