I'm trying to develop a for cycle to define a sequence of actions based on the lower cost.
To do that, I have defined an actions array. My idea is to compare each action in the array with another one, finding the cheaper path. When it's decided an action, after it's removed.
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);
}
}
When I run my code I obtain this result
2
The action is unload_plane Array lenght is6
The action is unload_plane Array lenght is6
The action is unload_plane Array lenght is6
The action is unload_plane Array lenght is6
The action is unload_plane Array lenght is6
The action is unload_plane Array lenght is6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at linkList.LinkedList.main(LinkedList.java:105)
So I think that the problem is where I remove the action for each loop, because seems not working. How can I fix it?
Aucun commentaire:
Enregistrer un commentaire