A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

fbz123456

初级黑马

  • 黑马币:54

  • 帖子:18

  • 精华:0

© fbz123456 初级黑马   /  2016-4-14 10:26  /  393 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


我有困惑  集合 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();
}
}

1 个回复

倒序浏览
没人理我  好尴尬啊  
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马