List中remove()⽅法的陷阱,被坑惨了(1),linux⽂件系统原理
blog.csdn/pelifymeng2/article/details/78085836
Java的List在删除元素时,⼀般会⽤ve(o)/remove(i)⽅法。在使⽤时,容易触碰陷阱,得到意想不到的结果。总结以往经验,记录下来与⼤家分享。
⾸先初始化List,代码如下:
package com.
《⼀线⼤⼚Java⾯试题解析+后端开发学习笔记+最新架构讲解视频+实战项⽬源码讲义》
【docs.qq/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
st;
import java.util.ArrayList;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
List list=new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(4);
System.out.println(list);
}
}
输出结果为[1, 2, 3, 3, 4]
1、普通for循环遍历List删除指定元素–错误
for(int i=0;i<list.size();i++){
(i)==3) ve(i);
}
System.out.println(list);
输出结果:[1, 2, 3, 4]
为什么元素3只删除了⼀个?本以为这代码再简单不过,可还是掉⼊了陷阱⾥,上⾯的代码这样写的话,元素3是过滤不完的。只要list中有相邻2个相同的元素,就过滤不完。
List调⽤remove(index)⽅法后,会移除index位置上的元素,index之后的元素就全部依次左移,即索引依次-1要保证能操作所有的数据,需要把index-1,否则原来索引为index+1的元素就⽆法遍历到(因为原来索引为index+1的数据,在执⾏移除操作后,索引变成index了,如果没有index-1的操作,就不会遍历到该元素,⽽是遍历该元素的下⼀个元素)。
如果这样,删除元素后同步调整索引或者倒序遍历删除元素,是否可⾏呢?
2、for循环遍历List删除元素时,让索引同步调整–正确!
for(int i=0;i<list.size();i++){
(i)==3) ve(i–);
}
System.out.println(list);
输出结果:[1, 2, 4]
3、倒序遍历List删除元素–正确!
for(int i=list.size()-1;i>=0;i–){
(i)==3){
}
}
System.out.println(list);
输出结果:[1, 2, 4]
4、foreach遍历List删除元素–错误
for(Integer i:list){
if(i==3) ve(i);
}
System.out.println(list);
抛出异常:java.util.ConcurrentModificationException
foreach 写法实际上是对的 Iterable、hasNext、next⽅法的简写。因此从List.iterator()源码着⼿分析,跟踪iterator()⽅法,该⽅法返回了 Itr 迭代器对象。
public Iterator iterator() {
return new Itr();
}
Itr 类定义如下:
private class Itr implements Iterator {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
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];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {linux系统安装步骤csdn
ve(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
通过代码我们发现 Itr 是 ArrayList 中定义的⼀个私有内部类,在 next、remove⽅法中都会调⽤checkForComodification ⽅法,该⽅法的 作⽤是判断 modCount != expectedModCount是否相等,如果不相等则抛出ConcurrentModificationException异常。
每次正常执⾏ remove ⽅法后,都会对执⾏expectedModCount = modCount赋值,保证两个值相等,那么问题基本上已经清晰了,在foreach 循环中执⾏ ve(item);,对 list 对象的 modCount 值进
⾏了修改,⽽ list 对象的迭代器的 expectedModCount 值未进⾏修改,因此抛出了ConcurrentModificationException异常。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。