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.length1; j>=0; j){ newArray[ii][jj] = arr[i][j]; jj++; } ii++; } return newArray; }
Problems Reply
I don't understand your loops' logic  shouldn't it be
for(int i=0; i<arr[0].length; i++){
for(int j=arr.length1; j>=0; j){
newArray[i][j] = arr[j][i];
}
}
Net of whether each index goes up, like i
here, or down, like j
here (and of whether either or both need to be "flipped" in the assignment, e.g using arr.length1j
in lieu of plain j
on one side of the =
in the assignment;), since arr
dimensions are arr.length
by arr[0].length
, and vice versa for newArray
, it seems to me that the first index on arr
(second on newArray
) must be the one spanning the range from 0 to arr.length1
included, and the other range for the other index.
This is a kind of "basic dimensional analysis" (except that "dimension" is used in a different sense than normally goes with "dimensional analysis" which refers to physical dimensions, i.e., time, mass, length, &c;). The issue of "flipping" and having each loop go up or down depend on visualizing exactly what you mean and I'm not the greatest "mental visualizer" so I think, in real life, I'd try the various variants of this "axis transposition" until I hit the one that's meant;).
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][M1r] = mat[r][c];
}
}
return ret;
}
Note a few things:
 It improves readability to refer to the dimensions of a MxN matrix as
M
andN
 It's traditional to use
r, c
instead ofi, 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
 Does not ensure that
 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 foreach 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.
 JAMA: http://math.nist.gov/javanumerics/jama/
 UJMP: http://www.ujmp.org/
Related questions
Technically, Java has array of arrays. Make sure you understand all the implications.
 Performance comparison of array of arrays vs multidimensional arrays

Java
Arrays.equals()
returnsfalse
for two dimensional arrays.
jj++ is run i*j times, and that can't be good at all.
Try to reset jj in the outer loop.
public class RotateMatrix {
static int index_of_rows;
static int index_of_columns;
static int number_of_rows;
static int number_of_columns;
public static void main(String[] args) {
int[][] matrix={{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};
index_of_rows = matrix.length 1;
index_of_columns = matrix[0].length 1;
number_of_rows = matrix.length;
number_of_columns = matrix[0].length;
RotateMatrix rm = new RotateMatrix();
rm.printGrid(matrix);//before rotation
rm.rotate360CW(matrix,rm);
}
public int[][] rotate90CW(int[][] matrix, RotateMatrix rm) {
int[][] newMatrix = new int[number_of_rows][number_of_columns];
int totalNumber = (number_of_rows) * (number_of_columns);
int[] intArray = createSingleArray(matrix,totalNumber);
int a =0;
for(int c=number_of_columns; c>=0; c)
{
for(int r=0; r<=number_of_rows; r++)
{
newMatrix[r][c] = intArray[a];
a++;
}
}
rm.printGrid(newMatrix);
return newMatrix;
}
public int[] createSingleArray(int[][] matrix, int totalNumber) {
int a=0;
int[] intArray = new int[totalNumber];
for(int b=0;b<=index_of_rows; b++)
{
for(int c=0; c<=index_of_columns;c++)
{
intArray[a] = matrix[b][c];
a++;
}
}
return intArray;
}
public void printGrid(int[][] matrix) {
StringBuilder sb = new StringBuilder("");
for(int i =0; i<=index_of_rows; i++)
{
System.out.println(sb.toString());//print each row
sb.delete(0, sb.length());//Then clear the row and build the next
for(int j=0; j<=index_of_columns;j++)
{
sb.append(matrix[i][j]+",");
}
}
System.out.println(sb.toString());
}
public int[][] rotate180CW(int[][] matrix, RotateMatrix rm)
{
return rm.rotate90CW(rm.rotate90CW(matrix, rm), rm);
}
public int[][] rotate270CW(int[][] matrix, RotateMatrix rm)
{
return rm.rotate90CW(rm.rotate90CW(rm.rotate90CW(matrix, rm), rm),rm);
}
public int[][] rotate360CW(int[][] matrix, RotateMatrix rm)
{
return rm.rotate90CW(rm.rotate90CW(rm.rotate90CW(rm.rotate90CW(matrix, rm), rm),rm),rm);
}
}