我那兒子列表變化 調試後知道,我不知道爲什麼它改變 這種修改會導致我的問題在我的算法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)
你明白什麼是'ConcurrentModification'表示,和是什麼原因導致它被拋出? –
http://stackoverflow.com/questions/9437139/java-java-util-concurrentmodificationexception –