导语

  • 编程过程中常常需要使用到集合,而ArrayList也是我们常常使用的,但是最近在一次删除和增加中出现了一些问题,分享记录下。
  • 请看下面两段代码,哪段代码会报错呢,或者都成功呢。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
List<String> arrayList1 = new ArrayList<>();
arrayList1.add("1");
arrayList1.add("2");
for (String s : arrayList1) {
if ("1".equals(s)) {
arrayList1.remove(s);
}
}
List<String> arrayList2 = new ArrayList<>();
arrayList2.add("2");
arrayList2.add("1");
for (String s : arrayList2) {
if ("1".equals(s)) {
arrayList2.remove(s);
}
}

程序运行结果如下:

arrayList1的remove方法成功执行,但是arrayList2的remove方法运行抛出ConcurrentModificationException异常。

寻找原因

  • 为了寻找原因,我们只能看源代码了。
    因为foreach的本质就是使用的迭代器Iterator,所有的Collection集合类都会实现Iterable接口。

  • 找到ArrayList类的iterator()方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /**
    * Returns an iterator over the elements in this list in proper sequence.
    *
    * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
    *
    * @return an iterator over the elements in this list in proper sequence
    */
    public Iterator<E> iterator() {
    return new Itr();
    }

    ArrayList使用自己的Itr内部类,并且实现了Iterator接口

  • 迭代器的本质是先调用hasNext()方法判断存不存在下一个元素,然后再使用next()方法取下一个元素

  • ArrayList的内部类Itr:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    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;

    Itr() {}

    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;
    return (E) elementData[lastRet = i];
    }
    ......
    }
  • 上面arraylist1为什么能remove成功呢,其实它只循环了一次,所以成功了。

  • 因为它在remove元素1之后,它的size - 1变成1,然后Itr内部的cursor变量由0变成1
    此时1 = 1,循环结束,所以成功了。

  • arraylist2为什么remove失败呢,因为他在循环第二次的时候,也remove成功了,但是第三次判断next的时候cursor的值为2导致不等于现在的size 1,所以执行了next方法,最重要的来了,之前remove的操作导致ArrayList的modCount值加1,然后Itr类中的expectedModCount保持不变,所以会抛出异常。

    1
    2
    3
    4
    final void checkForComodification() {
    if (modCount != expectedModCount)
    throw new ConcurrentModificationException();
    }
  • 同理可得,由于add操作也会导致modCount自增,所以不允许在foreach中删除、增加、修改ArrayList中的元素。

总结

  • 对此,推荐大家使用迭代器Iterator删除元素:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    List<String> arrayList2 = new ArrayList<>();
    arrayList2.add("2");
    arrayList2.add("1");
    Iterator<String> iterator = arrayList2.iterator();
    while (iterator.hasNext()) {
    String item = iterator.next();
    if ("1".equals(item)) {
    iterator.remove();
    }
    }
  • 如果存在并发操作,还需要对Iterator进行加锁操作。