2016-12-02 49 views
1

我那兒子列表變化 調試後知道,我不知道爲什麼它改變 這種修改會導致我的問題在我的算法java.util.ConcurrentModificationException在遞歸

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; 
} 

}

這裏是抽象類:

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) 
+2

你明白什麼是'ConcurrentModification'表示,和是什麼原因導致它被拋出? –

+0

http://stackoverflow.com/questions/9437139/java-java-util-concurrentmodificationexception –

回答

2

super.find_neighbors修改並返回兒子列表。裏面的循環的方法DLS遞歸調用它再次呼籲super.find_neighbors(修改當前迭代上面一個遞歸相同的列表)。由於列表在迭代過程中發生更改,因此引發異常。

+0

我明白這個函數會導致這個問題......我會嘗試創建一個列表的副本,以便兒子不會在Algo類中持有指向列表的指針。如果你有任何創造性的想法可以解決它,我會很高興知道它,謝謝 –

0

您是所作所爲內Algo.find_neighbours()一sons.clear()將導致ConcurrentModification在環。而且,由於find_neighbours()總是返回一個空List,試圖迭代它有什麼意義?

+1

總是返回一個空的列表? –

+0

不,我的錯誤,我錯過了部分代碼,但其中的clear()會導致循環中的ConcurrentModification。 –