Rotate two dimensional array 90 degrees clockwise

Posted by user69514 on Stack Overflow See other posts from Stack Overflow or by user69514
Published on 2010-05-10T00:36:52Z Indexed on 2010/05/10 0:48 UTC
Read the original article Hit count: 575

Filed under:
|
|
|

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;
    }

© Stack Overflow or respective owner

Related posts about arrays

Related posts about java