title: Unable to fix java.lang.ArrayIndexOutOfBoundsException for a shortest path problem
body: <p>I'm trying to develop a <strong>for cycle</strong> to define a <strong>sequence of actions</strong> based on the <strong>lower cost</strong>.</p>
  
  <p>To do that, I have defined an <strong>actions array</strong>. My idea is to <strong>compare</strong> each action in the array with another one, finding the cheaper path. When it's decided an action, after it's <strong>removed</strong>.</p>
  
  <pre><code>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 &gt; 0; i++, j++) {
  
          if(acts[j].g &gt;= acts[i].g) { // g is the cost, the last action parameter
  
  
              Node current_node = new Node(acts[i]);
              state.addState(1);
  
  
              List&lt;Action&gt; removeList = new ArrayList&lt;Action&gt;(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&lt;Action&gt; removeList = new ArrayList&lt;Action&gt;(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);
  
          }
  
      }
  </code></pre>
  
  <p>When I run my code I obtain this result</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>So I think that the problem is where I remove the action for each loop, because seems not working. How can I fix it?</p>