Search code examples
javaarraysmultidimensional-arrayindexoutofboundsexception

Java - how to rotate square array (two-dimensional) by 90 degree to the right?


So, how should it work. I input "n" and "m" to make two-dimensional matrix with n*m resolution. I need to rotate it by 90 degrees to the right to make it somehow like that image made in paint:

enter image description here

I wrote some code but i cant really make it working - looks like it is easy but every time i try to get it working i get "outofboundsexception". Here it is:

 import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s1 = new Scanner(System.in);
        int n = s1.nextInt();
        int m = s1.nextInt();
        System.out.println();
        int[][] array = new int[n][m];
        int[][] ar = new int[n][m];
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[n - 1].length; j++) {
                array[i][j] = s1.nextInt();
            }
        }
        System.out.println("INPUT ARRAY :");
        System.out.println();
        for(int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[n - 1].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
        /* here is the main actions with the array begin*/
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[n - 1].length; j++) {
                ar[i][j]=array[n - j - 1][i];
            }
        }
        /*the end of actions with the array*/
        System.out.println("TASK ARRAY :");
        System.out.println();
        for(int i = 0; i < ar.length; i++) {
            for (int j = 0; j < rr[n - 1].length; j++) {
                System.out.print(ar[i][j] + " ");
            }
            System.out.println();
        }
    }


}

What exactly am I doing wrong?


Solution

  • Here's the correct version of your program, with all the for-loop indices fixed, and the printing moved into a separate method.

    public class Main {
        static void print(String title, int[][] array) {
            System.out.println(title);
            System.out.println();
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    System.out.print(array[i][j] + " ");
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            Scanner s1 = new Scanner(System.in);
            int m = s1.nextInt();
            int n = s1.nextInt();
            System.out.println();
            int[][] array = new int[m][n];
            int[][] ar = new int[n][m];
            for(int i = 0; i < array.length; i++) {
                for(int j = 0; j < array[i].length; j++) {
                    array[i][j] = s1.nextInt();
                }
            }
    
            print("INPUT ARRAY :", array);
    
            /* here is the main actions with the array begin*/
            for(int i = 0; i < array.length; i++) {
                for(int j = 0; j < array[i].length; j++) {
                    ar[j][m - i - 1] = array[i][j];
                }
            }
    
            /*the end of actions with the array*/
            print("TASK ARRAY :", ar);
        }
    }