相关疑难解决方法(0)

清除子列表时出现ConcurrentModificationException

ConcurrentModificationExcrption当我清除主列表后的子列表时,为什么以下代码抛出,但如果我清除子列表然后清除主列表则不会?

ArrayList<Integer> masterList = new ArrayList<Integer>();
List<Integer> subList;

// Add some values to the masterList
for (int i = 0; i < 10; i++) {
    masterList.add(i * i);
}

// Extract a subList from the masterList
subList = masterList.subList(5, masterList.size() - 1);

// The below throws ConcurrentModificationException
masterList.clear();
subList.clear(); // Exception thrown in this line

// The below doesn't throw any exception
subList.clear();
masterList.clear(); // No exception thrown. Confused??
Run Code Online (Sandbox Code Playgroud)

java list

7
推荐指数
1
解决办法
1067
查看次数

标签 统计

java ×1

list ×1