爪哇 - 将元素添加到列表在遍历它 [英] Java - adding elements to list while iterating over it

查看:128
本文介绍了爪哇 - 将元素添加到列表在遍历它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想避免让ConcurrentModificationException的。我会怎么做呢?

I want to avoid getting ConcurrentModificationException. How would I do it?

推荐答案

您可以使用的ListIterator 有支持一个删除/迭代本身的过程中添加的方法。

You may use a ListIterator which has support for a remove/add method during the iteration itself.

ListIterator<Book> iter = books.listIterator();
while(iter.hasNext()){
    if(iter.next().getIsbn().equals(isbn)){
        iter.add(new Book(...));
    }
}

这篇关于爪哇 - 将元素添加到列表在遍历它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆