public class RotateImage { public void rotate(int[][] matrix) { if(matrix.length == 1 && matrix[0].length == 1) { return; } int n = matrix.length; for(int i = 0; i < n-1; i ++) { for(int j = i; j < n-1-i; j ++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n-1-j][i]; matrix[n-1-j][i] = matrix[n-1-i][n-1-j]; matrix[n-1-i][n-1-j] = matrix[j][n-1-i]; matrix[j][n-1-i] = temp; } } } public static void main(String[] args) { RotateImage ri = new RotateImage(); int[][] a = {{1,2,3},{4,5,6},{7,8,9}}; ri.rotate(a); //ri.rotate(a); //ri.rotate(a); //ri.rotate(a); for (int[] is : a) { for (int i : is) { System.out.print(i+" "); } System.out.println(" "); } } }