ArrayList 使用 forEach 遍历时删除元素会报错吗?

ArrayList 使用 forEach 遍历时删除元素会报错吗?,第1张

ArrayList使用forEach遍历的时候删除元素会报错吗?

其实不一定,如果删除的元素是倒数第二个则不会报错,

否则报错ConcurrentModificationException。

案例:

List<String>lists = new ArrayList<String>()

lists.add("1")

lists.add("2")

lists.add("3")

lists.add("4")

如果要删除等于“3”的元素,我们都知道ArrayList底层是类似数组的形式才存储数据的,生成一个元素后,后面的元素要往前移动,同时lists的size减1。这时lists变成[“1”,“2”,“4”],大小为3。

使用forEach遍历时:

for(String s :lists){if(s.equals("3")){

lists

.remove(s)    }}

//这是一颗语法糖,编译后相当于:for(Iterator i = lists.iterator()i.hasNext()){String s = (String)i.next()if(s.equals("3")){

list

.remove(s)}}

Iterator的hasNext()方法判断了size和当前下标cursor是否一样,一样则说明已经没有元素了。

如果remove了“3”这个元素之后,size会变成3,这时候遍历的下标cursor刚好是3,因此不会再进行下一次循环,直接结束了,此时元素“4”是没有被遍历到的。

假如lists中的元素是[“1”,“2”,“3”,“4”,“5”],即3不再是倒数第二个元素了呢?

此时会进行下一次循环,先判断i.hasNext(),发现当前下标cursor不等于size,执行i.next(),试图取出下一个值“4”,这时候就报错了,原因在i.next()中:

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]

}

final void checkForComodification() {

    if (modCount != expectedModCount)

      throw new ConcurrentModificationException()

}

Iterator取下一个值时候会先判断modCount是否和expectedModCount一样,不一样就报错。    

这里的modCount是删除的元素的数量计数,expectedModCount是Iterator期望的删除数量,使用Iterator的remove()方法的时候,Iterator会将调用ArrayList.this.remove(lastRet)删除元素同时使得modCount++,然后将modCount的值赋给expectedModCount,确保它们一样。

所以到这里我们就可以发现问题了,在forEach循环体里,我们直接使用的是lists.remove(“3”)的方法来删除元素,导致了expectedModCount和modCount不一致。

所以要在遍历的时候删除元素,不能使用forEach遍历的方式,要使用Iterator的方法。

String s= null

for(Iterator i = lists.iterator()i.hasNext()){

  s=(String)i.next()

  if(s.equals("3")){

      i.remove()

  }

}

还有一种方法是使用CopyOnWriteArrayList代替ArrayList,这是一种写时复制的容器,每次添加删除元素的时候都会复制一份旧的数据,新建一个新数据,在新数据进行修改后再修改旧数据的指针指到新数据。这样的话,遍历的数据其实都是第一份的旧数据,旧数据是没有变的,我们使用旧数据遍历,使用新数据判断值。

图:

介绍一种比较简单的批量 *** 作,一个批量添加一个批量删除:

Java代码:

public class User implements Serializable {

private Integer id

private String name

private String password

//setter and getter

}

对应的Mapper.xml

<ResultMap type="User" id="UserResultMap">

<id column="id" property="id" jdbcType="Integer"/>

<result column="name" property="name" jdbcType="VARCHAR" />

<result column="password" property="password" jdbcType="VARCHAR" />

</ResultMap>

Mapper.xml中对应的批量插入方法:(传入的是一个List集合)

<insert id="add" useGeneratedKeys="true" parameterType="java.util.List">

<selectKey resultType="long" keyProperty="id" order="AFTER">

select last_insert_id()

</selectKey>

insert into t_user (name,password) values

<foreach collection="list" item="item" index="index" separator=",">

(#{item.name},#{item.password})

</foreach>

</insert>

Mapper.xml中对应的批量删除的方法:(传入的是一个string字符串,ids)

<delete

id="delete" parameterType="java.lang.String">

delete from t_user where id in (“${_param}”);($好像是#号,记不太清了,可以试下)

</delete>

OK!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11450824.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存