黑马程序员技术交流社区

标题: 集合 hasnext 中游尺 判断 [打印本页]

作者: fbz123456    时间: 2016-4-14 10:26
标题: 集合 hasnext 中游尺 判断

我有困惑  集合 hasnext  中 游尺判断  如以下源码中红色  cursor 初值如何确定 又如何增长 不胜感激  谢谢
private class Itr implements Iterator {
int cursor ;
int lastRet = -1;
int expectedModCount = modCount;

public boolean hasNext() {
  return cursor != size();
}


public Object next() {
  checkForComodification();
  try {
   Object next = get(cursor);
   lastRet = cursor++;
   return next;
  } catch(IndexOutOfBoundsException e) {
   checkForComodification();
   throw new NoSuchElementException();
  }
}

public void remove() {
  if (lastRet == -1)
   throw new IllegalStateException();
   checkForComodification();

  try {
   AbstractList.this.remove(lastRet);
   if (lastRet < cursor)
    cursor--;
   lastRet = -1;
   expectedModCount = modCount;
  } catch(IndexOutOfBoundsException e) {
   throw new ConcurrentModificationException();
  }
}

final void checkForComodification() {
  if (modCount != expectedModCount)
   throw new ConcurrentModificationException();
}
}

作者: fbz123456    时间: 2016-4-15 15:49
没人理我  好尴尬啊  




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2