递归中的java.util.ConcurrentModificationException

递归中的java.util.ConcurrentModificationException,java,recursion,Java,Recursion,调试后我知道子列表已更改 我不知道为什么会改变 这种修改会给我的算法带来问题 public class IDS extends Algo { public State found = null; public IDS(char[][] input, int size) { super(input, size); } public String solve() { State root = new State(0, 0, 0, 0, "", 0, 0); for (in

调试后我知道子列表已更改 我不知道为什么会改变 这种修改会给我的算法带来问题

public class IDS extends Algo {
public State found = null;

public IDS(char[][] input, int size) {
    super(input, size);
}

public String solve() {
    State root = new State(0, 0, 0, 0, "", 0, 0);
    for (int i = 0; i < size * size; i++) {
        found = DLS(root, i);
        if (found != null) {
            return found.path + " " + found.dist;
        }
    }
    return "no path";
}

private State DLS(State node, int depth) {
    if (depth == 0 && super.inp[node.row][node.col] == 'G') {
        return node;
    }
    if (depth > 0) {
        // List<State> sons = super.find_neighbors(node.row, node.col, node.prod_time);
        // for (int j = 0; j < sons.size(); j++) {
        //State cur = sons.get(j);
        for(State cur : super.find_neighbors(node.row, node.col, node.prod_time)){
            cur.addCost(node.dist);
            cur.path = cur.addpath(node.path);
            found = DLS(cur, depth - 1);
            if (found != null) {
                return found;
            }
        }
    }
    return null;
}

您正在Algo.find_neights()中执行一个sons.clear(),这将导致循环中的ConcurrentModification。此外,既然find_neights()总是返回一个空列表,那么尝试遍历它有什么意义呢

super.find_neights修改并返回子列表。在循环中递归调用方法DLS,该方法再次调用super.find_neighbories(修改当前在上面一个递归中迭代的相同列表)。因为列表在迭代过程中发生了更改,所以会引发异常。

您是否了解
ConcurrentModification
指示的内容以及引发异常的原因?始终返回空列表?不,我的错,我遗漏了部分代码,但清除()在循环中,它将导致ConcurrentModification。我知道这个函数会导致问题。。。我将尝试创建列表的副本,这样sons就不会在Algo类中持有指向列表的指针。如果你有什么创造性的想法可以解决这个问题,我很高兴知道,谢谢
abstract class Algo {
public static Map<Integer,String> ways = new TreeMap<>();
protected char[][]inp;
protected int size;
private Map<Character,Integer>costs = new HashMap<>();
public List<State>sons = new ArrayList<>();

public Algo(){}
public Algo(char[][] input,int size){
   inp = input;
    this.size = size;
    ways.put(1,"R");
    ways.put(2,"RD");
    ways.put(3,"D");
    ways.put(4,"LD");
    ways.put(5,"L");
    ways.put(6,"LU");
    ways.put(7,"U");
    ways.put(8,"RU");
    costs.put('R',1);
    costs.put('D',3);
    costs.put('H',10);
    costs.put('G',0);
    costs.put('S',0);
}
private void addDirection(int row,int col,int prod, int loc){
    int cos = costs.get(inp[row][col]);
    State s = new State(row,col,0,prod,"",loc,cos);
    sons.add(s);
}
public abstract String solve();
//gets the parent i,j and the prod_t of the sons
public List<State> find_neighbors(int i, int j,int prod_t){
    List<String>openD = new ArrayList<>();
    sons.clear();

    if(j+1 < size) {//right
        if (inp[i][j + 1] != 'W') {
            addDirection(i, j + 1, prod_t, 1);
            openD.add("R");
        }
    }
    if(i-1 >= 0) {
        if (inp[i - 1][j] != 'W') {//up
            addDirection(i - 1, j, prod_t, 7);
            openD.add("U");
        }
    }
    if(i+1 < size) {
        if (inp[i + 1][j] != 'W') {//down
            addDirection(i + 1, j, prod_t, 3);
            openD.add("D");
        }
    }
    if(j-1 >= 0){
        if(inp[i][j-1]!='W'){//left
            addDirection(i,j-1,prod_t,5);
            openD.add("L");
        }
    }

    if(openD.contains("L")){
        if(openD.contains("U")){
            if(inp[i-1][j-1]!='W'){
                addDirection(i-1,j-1,prod_t,6);
            }
        }
        if(openD.contains("D")){
            if(inp[i+1][j-1]!='W'){
                addDirection(i+1,j-1,prod_t,4);
            }
        }
    }
    if(openD.contains("R")){
        if(openD.contains("U")) {
            if (inp[i - 1][j + 1] != 'W') {
                addDirection(i - 1, j + 1, prod_t, 8);
            }
        }
        if(openD.contains("D")){
            if(inp[i+1][j+1]!='W'){
                addDirection(i+1,j+1,prod_t,2);
            }
        }
    }
    return sons;
}
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at IDS.DLS(IDS.java:33)
at IDS.solve(IDS.java:17)
at ex1.main(ex1.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)