title: Rotate a 2d matrix to the right
body: <p>I want a 2d matrix to rotate to the right, it compiles fine but when I try to the run it says that the <em>array index is out of bounds exception</em>. For example, I want <code>{{10,20,30},{40,50,60}}</code> to rotate into <code>{{40,10},{50,20},{60,30}}</code>:</p>
  <pre class="lang-java prettyprint-override"><code>public static int[][] rotate(int[][] m) {
      int[][] rotateM = new int[m[0].length][m.length];
      for (int i = 0; i &lt; m.length; i++) {
          for (int j = 0; j &lt; m[0].length; j++) {
              rotateM[i][j] = m[j][m.length - i - 1];
          }
      }
      return rotateM;
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>public static void main(String[] args) {
      int[][] m = {
              {10, 20, 30},
              {40, 50, 60}};
      System.out.println(Arrays.toString(rotate(m)));
  }
  </code></pre>