title: How do I get this 2D Array to rotate to the right 90 degrees?
body: <p>So I have a 2D array, and it is supposed to rotate to the right 90 degrees, but instead it rotates to the left.  Really can't figure out why</p>
  
  <p>public class CrackCode16 {</p>
  
  <pre><code>public static void main (String args[]){
      int [] [] oldarray = new int [3][3];
  
      int value = 1;
  
      for (int i = 0; i &lt; 3; i++){
          for (int j =0; j&lt;3; j++){
              oldarray[i][j] = value;
              value++;
          }
      }
  
  
      for (int i = 0; i &lt; 3; i++){
          for (int j =0; j&lt;3; j++){
              System.out.print(oldarray[i][j] + "\t");
          }
          System.out.println("");
      }
  
  
      oldarray = rotate(oldarray, 3);
  
  
      System.out.println("");
      for (int i = 0; i &lt; 3; i++){
          for (int j =0; j&lt;3; j++){
              System.out.print(oldarray[i][j] + "\t");
          }
          System.out.println("");
      }
  
  }
  
  public static int [][] rotate (int [][] passedIn, int n){
  
      int [][] newarray = new int [n][n];
  
      for (int i = 0; i &lt; n; i++){
          for (int j =0; j&lt;n; j++){
              newarray[i][j] = passedIn [j][n-1-i];
          }
      }   
  
      return newarray;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Output:</p>
  
  <p>1   2   3<br>
  4   5   6<br>
  7   8   9   </p>
  
  <p>3   6   9<br>
  2   5   8<br>
  1   4   7   </p>