­

ArrayList为什么要自己实现迭代器

  • 2019 年 10 月 30 日
  • 笔记

ArrayList的父类AbstractList已经实现了Iterator接口,为什么ArrayList还要自己实现Iterator接口呢?

ArrayList实现Iterator,是为了避免ArrayList在迭代过程中数组结构发生变化的而产生的问题,这个处理机制称为Fail-Fast机制,实际是一个乐观锁,实现如下。

ArrayList有一个modCount属性,在add(),remove()执行开始,modCount++。ArrayList创建迭代器对象时,会复制当前modCount到expectedModCount,迭代器每次执行next(),remove(),forEachRemaining()时,都判断modCount是否与expectedModCount相同,若不相同,则抛出异常。

    private class Itr implements Iterator<E> {          int cursor;       // index of next element to return          int lastRet = -1; // index of last element returned; -1 if no such          int expectedModCount = modCount;            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;              return (E) elementData[lastRet = i];          }            final void checkForComodification() {              if (modCount != expectedModCount)                  throw new ConcurrentModificationException();          }      }

ArrayList不是线程安全的,Fail-Fast不是来解决多线程的问题的。这个的实际意义在于单线程时,新手在foreach中做add(),remove()操作。如果要解决多线程,要在add()和remove()中加上modCount的效验了。