gpt4 book ai didi

java - Java BO 中的 ConcurrentModificationException

转载 作者:行者123 更新时间:2023-12-01 17:26:21 25 4
gpt4 key购买 nike

我在此代码中遇到 java.util.ConcurrentModificationException,但似乎无法理解原因或修复它。

在 BO 中我有这个(权限是 BO 内的数组列表)

public void setPrivilegename(String privilegename) {
this.privileges.add(privilegename);
}

List<PrivilegeObjectBO> arraylist = new ArrayList<PrivilegeObjectBO>();if (rs != null) {
while (rs.next()) {
for (BO bo : arraylist) {
if (bo.getRolepkey()==rs.getInt("ROLE_PKEY")) {
bo.setPrivilegename(rs.getString("SP.NAME"));

}else{
BO newobj = new BO();
newobj.setRolepriviledgepkey(rs.getInt("PRIVILEGE_PKEY"));
newobj.setProticolpkey(protocol);
newobj.setPrivilegename(rs.getString("SP.NAME"));
newobj.setRolepkey(rs.getInt("SRP.ROLE_PKEY"));
arraylist.add(newobj);
}
}
}
}

最佳答案

根据 ArrayList javadoc

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

for (BO bo : arraylist)

上面的 for-each 循环获取列表的 Iterator 和

  arraylist.add(newobj);

您尝试在不使用迭代器自己的方法的情况下修改列表,这会导致 ConcurrentModificationException

这里是SO discussion on possible solutions .

关于java - Java BO 中的 ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14734147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com