-1

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?

Mike994
  • 55
  • 8
  • Your j and i will always be the same. – Joel Oct 28 '19 at 14:44
  • And how can I fix it? – Mike994 Oct 28 '19 at 14:46
  • A) you have to really understand what your algorithm is supposed to do B) you have to debug why it is getting in such an "error" condition ... and unrelated: please read about java naming conventions. You dont use "_" in normal variable names, instead, you go camelCase in Java. – GhostCat Oct 28 '19 at 15:33
  • And that exception means: you are using an invalid index on an array. Part of your homework is to **find out** how to do things properly. – GhostCat Oct 28 '19 at 15:34

1 Answers1

0

To begin with, change removeList.removeAll(Arrays.asList(i)) to removeList.remove(i) or removeList.remove(Arrays.asList(acts).get(i)). If you check the output, your code is not removing the elements and that is the reason why the length is printed as 6 every time.

Next Problem:

 for(int i = 0, j = 0; acts.length > 0; i++, j++)        
    if(acts[j].g >= acts[i].g) 

The initial size of the array acts based on your cade is 6. But to make the explanation smaller, let's just consider the initial as 3 (like you have included only 3 actions to compare).

What you want to do is compare each element with the other in array to find the cheapest while removing an action with each iteration. Let's see what's going wrong with your implementation.

Initial Size of Acts: 3

  1. 1st iteration: acts size:3, i=0, j=0 --->if(acts[j].g >= acts[i].g) compares acts[0] with acts[0] which will always be equal and nothing is being removed like I explained above. Prints acts[0] and length is 3
  2. 2nd iteration: acts size:2, i=1, j=1 --->if(acts[j].g >= acts[i].g) compares acts[1] with acts[1] which will always be equal and none removed. Prints acts[1] and array size is 3 again

Before Proceeding, Think for a while what I am getting at? I bet you will figure it out otherwise well, continue reading :)

  1. 3rd iteration: acts size:1, i=1, j=1 --->if(acts[j].g >= acts[i].g) compares acts[1] with acts[1] which will always be equal and array size reduces by 1 since you removed an action. Prints acts[3] and array size like you already guessed is 3

So, Now? We have already reached the last element in your array and the size has not reduced at all. Therefore, acts.length > 0 will always be true (control will never exit for loop) and your array will go for another iteration.

  • 4th iteration: acts size:1, i=4, j=4 ---> if(acts[j].g >=acts[i].g) compares acts[4] with acts[4] and array does not have a 4th element and it causes the famous Array Index Out Of Bounds exception because you are trying to access an index that does not exist. :)

Concluding: We now have the reason why it throws an error and yes, we also realized your implementation is always comparing the values at any index to itself so if(acts[j].g >= acts[i].g) will always be true which is a flaw in the code.

Mathews Mathai
  • 1,707
  • 13
  • 31