int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
Itr() {} // 假设游标不等于size 代表还有下一个元素 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;
// 更新lastRet游标为以后数组下标
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(); } } }
(责任编辑:admin)