title: move element to end of java array
body: <p>solving something in algoexpert and getting weird index out of bound exception:</p>
  <p>the question is simply to take an array and another int and put all the numbers that equal to this int in the end of the array, like this:</p>
  <p>array: [2, 1, 2, 2, 2, 3, 4, 2]
  integer toMove: 2</p>
  <p>output: [1,3,4,2,2,2,2,2]</p>
  <p>so I wrote this algorithm:</p>
  <pre><code>class Program {
    public static List&lt;Integer&gt; moveElementToEnd(List&lt;Integer&gt; array, int toMove) {
      // Write your code here.
          int newArr[] = new int[array.size()];
          int lastIndertedNotToMoveIdx = 0;
          int endOfArrayIdx = array.size();
          // ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
          for (int i = 0; i &lt;= array.size(); i++) {
              if (array.get(i) == toMove) {
                  newArr[endOfArrayIdx] = array.get(i);
                      endOfArrayIdx = endOfArrayIdx-1;
              } else {
                  newArr[lastIndertedNotToMoveIdx] = array.get(i);
                  lastIndertedNotToMoveIdx++;
              }
          }
                  
        List&lt;Integer&gt; ret_list = new ArrayList&lt;Integer&gt;();
        for(Integer num:newArr) {
           ret_list.add(num);
        }
      return ret_list;
    }
  }
  </code></pre>
  <p>but here <code>newArr[endOfArrayIdx] = array.get(i);</code> I get this message:</p>
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8
      at Program.moveElementToEnd(Program.java:12)
      at AeJsonTest.getActual(AeJsonTest.java:27)
      at Main.main(Main.java:57)
  </code></pre>
  <p>not sure why...</p>