views:

271

answers:

3

I have a two dimensional array that I need to rotate 90 degrees clockwise, however I keep getting arrayindexoutofbounds...

public int[][] rorateArray(int[][] arr){

        //first change the dimensions vertical length for horizontal length
        //and viceversa
        int[][] newArray = new int[arr[0].length][arr.length];

        //invert values 90 degrees clockwise by starting from button of
        //array to top and from left to right
        int ii = 0;
        int jj = 0;
        for(int i=0; i<arr[0].length; i++){
            for(int j=arr.length-1; j>=0; j--){
                newArray[ii][jj] = arr[i][j];

                jj++;
            }
            ii++;
        }

        return newArray;
    }
+3  A: 

jj++ is run i*j times, and that can't be good at all.

Try to reset jj in the outer loop.

Jens Björnhager
+1  A: 
Alex Martelli
this turns it counterclockwise, but it works thanks
@user, you're welcome -- I did suspect I was missing some flipping and/or upside-down vs downside-up for either (or both?-) indices, as I mentioned in the 2nd paragraph;-).
Alex Martelli
newArray[i][j] = arr[j][i]; transposes the matrix. It should be newArray[i][length-1-j] = arr[j][i];
Maciej Hehl
@MacieJ is correct. See my answer for a more thorough treatment of the problem.
polygenelubricants
+3  A: 

Here's a standard matrix clockwise rotation code:

static int[][] rotateCW(int[][] mat) {
    final int M = mat.length;
    final int N = mat[0].length;
    int[][] ret = new int[N][M];
    for (int r = 0; r < M; r++) {
        for (int c = 0; c < N; c++) {
            ret[c][M-1-r] = mat[r][c];
        }
    }
    return ret;
}

Note a few things:

  • It improves readability to refer to the dimensions of a MxN matrix as M and N
  • It's traditional to use r, c instead of i, j to index row and column of a matrix
  • This is not the most robust implementation:
    • Does not ensure that mat is a valid MxN matrix, M>0, N>0
  • Use an explicit mapping formula instead of extraneous local variables
    • Makes program less complex and more readable

Here's a test harness:

import java.util.Arrays;
//...

static void printMatrix(int[][] mat) {
    System.out.println("Matrix = ");
    for (int[] row : mat) {
        System.out.println(Arrays.toString(row));
    }
}
public static void main(String[] args){
    int[][] mat = {
        { 1, 2, 3 },
        { 4, 5, 6 }
    };
    printMatrix(mat);
    // Matrix = 
    // [1, 2, 3]
    // [4, 5, 6]

    int[][] matCW = rotateCW(mat);
    printMatrix(matCW);
    // Matrix = 
    // [4, 1]
    // [5, 2]
    // [6, 3]
}

Note the use of the for-each loop and java.util.Arrays in printMatrix. You should definitely familiarize yourself with them if you're working with arrays a lot in Java.

Links to Java matrix libraries

If you're working with matrices a lot, you may want to consider using a specialized matrix library instead.

Related questions

Technically, Java has array of arrays. Make sure you understand all the implications.

polygenelubricants