从列表中删除项目 [英] Removing items from a list

查看:114
本文介绍了从列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

循环浏览列表时,我想根据条件删除列表中的项目。请参阅下面的代码。

While looping through a list, I would like to remove an item of a list depending on a condition. See the code below.

这给了我一个 ConcurrentModification 例外。

for (Object a : list) {
    if (a.getXXX().equalsIgnoreCase("AAA")) {
        logger.info("this is AAA........should be removed from the list ");
        list.remove(a);
    }
}

如何做到这一点?

推荐答案

您需要使用 Iterator 并致电 remove() on iterator 而不是使用进行循环。

You need to use Iterator and call remove() on iterator instead of using for loop.

这篇关于从列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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