title: Shifting all elements of an array back one in Java
body: <p>I am writing a code that will shift all elements of an array back one, and move the last element to the front of the array.</p>
  
  <p>I pretty much want my program to do this:</p>
  
  <pre><code>int[] array = new int[] {1, 2, 3};
  // do something such that array will become {3, 1, 2}
  </code></pre>
  
  <p>My code is as follows:</p>
  
  <pre><code>int[] array = new int[3];
  
  
  for(int i = 0; i &lt; array.length; i++)
  {
    array[0] = 1;
    array[1] = 2;
    array[2] = 3;
    int last = array[array.length-1];
    array[i] = array[i+1];
    array[0] = last;
  
    System.out.println(array[i]);
  }
  </code></pre>
  
  <p>I thought that adding 1 to "i" for the array, and storing the last element of the array in "last" and assigning it to array[0] would do the trick, but I'm just getting {3, 3} in the output with "java.lang.ArrayIndexOutOfBoundsException: 3."</p>