java - Java 循环遍历 arraylist 并使用匹配的代码删除 item 类型的任何对象
问题描述
我正在尝试执行遍历 item 类型对象的数组列表的任务。每个项目对象都有一个代码变量。如果已经有一个带有代码的对象,那么它将从列表中删除。
代码:
//Search list for item based on code.
//If item with code is found, it is removed/deleted from the list.
//Else user is told "No item Found"
public void deleteItem(String code)
{
boolean found = false;
for(Item item : items)
{
if(item.getCode().equals(code))
{
found = true;
items.remove(item);
}
}
if(found)
{
System.out.println("Found and removed item with code: " + code);
}
else
{
System.out.println("No item with code" + code + "found");
}
}
我得到 java.util.ConcurrentModificationException
请问有什么帮助吗?
解决方案
推荐阅读
- javascript - 试图用ajax插入多选输入
- reactjs - 如何在数据加载时重新呈现自定义挂钩的结果
- c++ - 如果我的结构中有堆分配的成员,我是否必须在堆中分配结构本身?
- atom-editor - 如何比较 2 个 txt 文件的差异并输出到新的 txt 文件
- python - 在具有两个以上处理器组的双套接字系统上通过 ctypes 在 Python 中使用 GetLogicalProcessorInformationEx()
- java - 如何在不使用管理外部存储权限的情况下在 android 11 范围存储中获取我的手机存储中可用的所有 pdf 文件
- python - pandas df.to_sql 到 Oracle 数据库数据类型不一致
- python - 同时比较一个列表和另外两个列表
- typescript - 为什么 TypeScript 在分配给新的空数组时不暗示数组类型?
- python - 为什么我的 textBrowser 在显示字符串时会换行