title: Java: how to rotate a non squared 2D array
body: <p>I have this as a color array:</p>
  
  <pre><code>public RGBImage(int width, int height, RGBColor background) {
        pixel =  new RGBColor[width][height];
        this.w = width;
        this.h = height;
        this.b = background;
  
        for(x = 0; x &lt; width; x++){
            for(y = 0; y &lt; height; y++){
                pixel[x][y] = b;
            }
        }
  </code></pre>
  
  <p>and i want to rotate it, right, the code is already doing good regarding square matrix thanks to <em>@Oblivion Creations</em>, but I'm getting <strong>outofbounds errors</strong> when using it non squared matrixes  </p>
  
  <pre><code>   public void rotateRight() {
        RGBColor[][] mirror = new RGBColor[h][w];
                for(int i = 0 ; i &lt; h; i++){
                    for(int j = 0 ; j &lt; w; j++){
                        mirror[i][j] = pixel[j][w-i-1];
                    }
                }
              pixel = mirror;
    }
  </code></pre>