privateclassArrayListIteratorimplementsIterator<E> {privateint position; // current position within the listprivateboolean removeOK; // whether it's okay to remove now// post: constructs an iterator for the given listpublic ArrayListIterator() {
position = 0;
removeOK = false;
}
// post: returns true if there are more elements left, false otherwisepublicboolean hasNext() {
return position < size();
}
// pre : hasNext() (throws NoSuchElementException if not)// post: returns the next element in the iterationpublic E next() {
if (!hasNext()) {
thrownew NoSuchElementException();
}
E result = elementData[position];
position++;
removeOK = true;
return result;
}
// pre : next() has been called without a call on remove (throws// IllegalStateException if not)// post: removes the last element returned by the iteratorpublicvoid remove() {
if (!removeOK) {
thrownew IllegalStateException();
}
ArrayList.this.remove(position - 1);
position--;
removeOK = false;
}
}