Skip to content

Instantly share code, notes, and snippets.

@orionll
Created September 11, 2018 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orionll/f29cf8788bb735a8ac4aca25edcb0cf4 to your computer and use it in GitHub Desktop.
Save orionll/f29cf8788bb735a8ac4aca25edcb0cf4 to your computer and use it in GitHub Desktop.
Bug in Array Iterator
public final class ArrayIterator<A> implements Iterator<A> {
private int i;
private final A[] array;
ArrayIterator(final A[] array) {
this.array = array;
}
@Override
public boolean hasNext() {
return (i < array.length);
}
@Override
public A next() {
try {
return array[i++];
} catch (IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment