无法修复最短路径问题的java.lang.ArrayIndexOutOfBoundsException
我正在尝试为周期开发一个,以基于较低成本定义一个行动序列无法修复最短路径问题的java.lang.ArrayIndexOutOfBoundsException,java,arrays,for-loop,if-statement,Java,Arrays,For Loop,If Statement,我正在尝试为周期开发一个,以基于较低成本定义一个行动序列 public static void main(String[] args) { LinkedList list = new LinkedList(); Action load_truck = new Action("load_truck","a","a","a", 4); Action load_plane = new Action("load_plane","a","a
public static void main(String[] args)
{
LinkedList list = new LinkedList();
Action load_truck = new Action("load_truck","a","a","a", 4);
Action load_plane = new Action("load_plane","a","a","a", 1);
Action fly = new Action("fly","a","a","a", 2);
Action drive = new Action("drive","a","a","a", 5);
Action unload_truck = new Action("unload_truck","a","a","a", 6);
Action unload_plane = new Action("unload_plane","a","a","a", 3);
Action acts[] = {load_truck, load_plane, fly, drive, unload_truck, unload_plane};
State state = new State(0);
System.out.println(acts[2].g); // this is a control to check if code is working until here
for(int i = 0, j = 0; acts.length > 0; i++, j++) {
if(acts[j].g >= acts[i].g) { // g is the cost, the last action parameter
Node current_node = new Node(acts[i]);
state.addState(1);
List<Action> removeList = new ArrayList<Action>(Arrays.asList(acts));
removeList.removeAll(Arrays.asList(i));
acts = removeList.toArray(acts);
System.out.println("The action is "+ " "+ acts[i].name + " Array lenght is" + acts.length);
}else {
Node current_node = new Node(acts[j]);
state.addState(1);
List<Action> removeList = new ArrayList<Action>(Arrays.asList(acts));
removeList.removeAll(Arrays.asList(j));
acts = removeList.toArray(acts);
System.out.println("The action is" + " "+ acts[j].name + " Array lenght is" + acts.length);
}
}
为此,我定义了一个操作数组。我的想法是将阵列中的每个动作与另一个动作进行比较,找到更便宜的路径。当它决定一个动作时,在它被删除之后
public static void main(String[] args)
{
LinkedList list = new LinkedList();
Action load_truck = new Action("load_truck","a","a","a", 4);
Action load_plane = new Action("load_plane","a","a","a", 1);
Action fly = new Action("fly","a","a","a", 2);
Action drive = new Action("drive","a","a","a", 5);
Action unload_truck = new Action("unload_truck","a","a","a", 6);
Action unload_plane = new Action("unload_plane","a","a","a", 3);
Action acts[] = {load_truck, load_plane, fly, drive, unload_truck, unload_plane};
State state = new State(0);
System.out.println(acts[2].g); // this is a control to check if code is working until here
for(int i = 0, j = 0; acts.length > 0; i++, j++) {
if(acts[j].g >= acts[i].g) { // g is the cost, the last action parameter
Node current_node = new Node(acts[i]);
state.addState(1);
List<Action> removeList = new ArrayList<Action>(Arrays.asList(acts));
removeList.removeAll(Arrays.asList(i));
acts = removeList.toArray(acts);
System.out.println("The action is "+ " "+ acts[i].name + " Array lenght is" + acts.length);
}else {
Node current_node = new Node(acts[j]);
state.addState(1);
List<Action> removeList = new ArrayList<Action>(Arrays.asList(acts));
removeList.removeAll(Arrays.asList(j));
acts = removeList.toArray(acts);
System.out.println("The action is" + " "+ acts[j].name + " Array lenght is" + acts.length);
}
}
所以我认为问题在于我删除了每个循环的操作,因为它似乎不起作用。我怎样才能修好它
首先,将removeList.removeAll(Arrays.asList(i))
更改为
removeList.删除(i)
或
removeList.remove(Arrays.asList(acts.get(i))
。如果你检查输出,
您的代码没有删除元素,这就是
每次打印长度为6
下一个问题:
for(int i = 0, j = 0; acts.length > 0; i++, j++)
if(acts[j].g >= acts[i].g)
根据您的cade,数组的初始大小为6。但是为了使解释变小,让我们只考虑初始为3(就像你只包含了3个动作来比较)。
您要做的是将数组中的每个元素与其他元素进行比较,以
在每次迭代中删除操作时查找最便宜的。让我们
看看你的实现出了什么问题
Acts的初始规模:3
if(acts[j].g>=acts[i].g)
将acts[0]与
acts[0]将始终相等,并且不会像
我在上面解释过。
打印acts[0],长度为3if(acts[j].g>=acts[i].g)
将acts[1]与
法案[1]将始终平等且不删除。
打印acts[1],数组大小再次为3if(acts[j].g>=acts[i].g)
将acts[1]与
acts[1]将始终相等,并且数组大小减少1,因为
您删除了一个操作。
打印行为[3],数组大小如您所猜测的是3acts.length>0
将始终为真(控制将永远不会退出循环),并且您的数组将进行另一次迭代
- 第四次迭代:acts大小:1,i=4,j=4-->
将acts[4]与acts[4]进行比较,并且数组没有 第4个元素,它导致著名的if(acts[j].g>=acts[i].g)
异常,因为您试图访问的索引不存在 存在:)李>数组索引超出范围
如果(acts[j].g>=acts[i].g)
总是正确的,这是代码中的一个缺陷。你的j和我总是一样的。我该如何修复它呢?a)你必须真正理解你的算法应该做什么B)你必须调试它为什么会出现这样的“错误”情况。。。与此无关:请阅读java命名约定。在普通变量名中不使用“\”,而是在Java中使用camelCase。这种异常意味着:在数组中使用的索引无效。你的家庭作业的一部分是找出如何正确地做事。