ConcurrentModificationExceptionHello在此行for (Transaction t : c.getTransactions())(第 64 行)上抛出 Exception 2 次迭代。我理解错误的本质——它发生在迭代器的元素在循环中被删除时。试图将 remove 更改为removeIF,但同样的错误。这是一个独特的问题,因为它发生在满足其解决方案的所有要求时。
private static void iteration() {
Integer n = 1;
Integer k = 1;
Double maxProfit;
Integer clusterInd;
while (k > 0) {
System.out.println("Итерация " + n);
n++;
k = 0;
for (int i=0;i<clusters.size();i++) {
Cluster c = clusters.get(i);
for (Transaction t : c.getTransactions()) {//ошибку кидает здесь на 2 итерации
maxProfit = profit();
clusterInd = -1;
c.deleteTransaction(t);
int j = 0;
for (Cluster cl : clusters) {
if (j != i) {
cl.addTransaction(t);
Double p = profit();
if (p > maxProfit) {
maxProfit = p;
clusterInd = j;
}
cl.deleteTransaction(t);
}
j++;
}
if (clusterInd == -1){
clusters.get(i).addTransaction(t);
}else {
k++;
clusters.get(clusterInd).addTransaction(t);
}
}
}
}
System.out.println(k);
}
删除事务的函数:
public void deleteTransaction(Transaction m) {
if (this.count > 0) {
String[] trans = m.getTrans();
for (String s : trans) {
this.square--;
if (freq.containsKey(s)) {
if (freq.get(s) > 0) {
this.freq.put(s, freq.get(s) - 1);
if (this.freq.get(s) == 0) {
this.width--;
freq.entrySet().removeIf(entry -> entry.getKey().equals(s));
}
}
}
}
this.count--;
if (this.count > 0) {
this.height = (double) this.square / this.width;
} else {
this.height = 0.0;
}
transactions.removeIf(m::equals);
}
}
日志:
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:939)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:893)
at com.lab6.ClopeAlgorithm.iteration(ClopeAlgorithm.java:64)
at com.lab6.ClopeAlgorithm.main(ClopeAlgorithm.java:96)
代替 foreach 循环,使用显式迭代器及其remove方法。
建议的解决方案:更改
for (Transaction t : c.getTransactions())为让我们创建一个小地图:
以下表达式将引发错误:
Java 8+:
爪哇 < 8:
不会抛出错误的表达式:
仅适用于 1 个删除,如果添加另一个
if要删除,则会抛出错误NoSuchElementException如果我们想删除两个或更多元素,我们可以使用:
要删除、添加、更改,最好使用
switch:但同样,这一切都有副作用。
如果某些物体飞入存储,则在:
我们得到
ConcurrentModificationException.在您的情况下,您可以使用 like
mutex。这样就可以不怕它飞了。
Foreach 是一种处理集合元素的机制,而不是集合本身。使用 foreach 修改集合不再正确。而写拐杖和自行车来“扩大语言设计的可能性”根本就是个坏主意。这种方法将破坏将实现 A 替换为实现 B 的能力,而无需手鼓跳舞并重写在 foreach 中修改集合的所有代码。
这是经典的迭代器方法之一:
取自这里:
https://habr.com/post/325426/#comment_10149968