Java中使用addAll方法,抛出ConcurrentModificationException,请问问题出在哪儿?怎么解决?

2025-04-09 02:07:34
推荐回答(2个)
回答1:

list.clear(); // 这句话的含义是 清空list,而上面的al1~al3都是由list而来,故error

上面clear去掉,list重定向个对象,即可 list = new ArrayList();
// new 生成的对象放在堆中,当list重定向之后,之前的那个对象依然存在,除非等到程序走完,gc会去清空堆中空间.故程序可运行.不知道这样的解释能不能看懂!

望知悉~

回答2:

subList方法返回的List和原来的List共享数据
Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive.