ConcurrentModificationException 没有发生

ConcurrentModificationException not happening

我有以下代码:

List list = getItems(); //where getItems() returns List<Item>


        do {
            list.add(adPosition);
            adPosition = adPosition + (AD_REPEAT_VALUE + 1);
            adsNumber++;
        } while (adsNumber < MAX_NUMBER_OF_ADS && adPosition < list.size());

据我所知,这段代码应该因 ConcurrentModificationException 而崩溃,因为列表大小已修改。但它不会崩溃。这怎么可能?!

Concurrent Modification 当我们在 List 对象上使用迭代器(或基本上迭代列表)对象时抛出异常,同时实际列表发生变化。

向列表中添加元素是完全合法的。请参阅有关 ConcurrentModificationException 的文档:当您同时添加和迭代列表时会抛出此问题:

Iterator it = list.iterator();
while (it.hasNext()) {
    list.add(elem); // ConcurrentModificationException thrown here
}