Java-如何将方形数组(二维)向右旋转90度?

时间:2018-10-01 19:45:49

标签: java arrays multidimensional-array indexoutofboundsexception

所以,它应该如何工作。我输入“ n”和“ m”来制作具有n * m分辨率的二维矩阵。 我需要将其向右旋转90度,以使其类似于油漆中的图像:

enter image description here

我写了一些代码,但是我无法真正使它工作-看起来很简单,但是每次我尝试使其工作时,我都会得到“ outofboundsexception”。在这里:

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


}

我到底在做什么错?

2 个答案:

答案 0 :(得分:-1)

这是您程序的正确版本,所有for循环索引均已固定,并且打印已移至单独的方法中。

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

答案 1 :(得分:-3)

`double x1 = point.x-center.x; 双重y1 = point.y-center.y;

double x2 = x1 * Math.cos(angle)-y1 * Math.sin(angle)); double y2 = x1 * Math.sin(angle)+ y1 * Math.cos(angle));

point.x = x2 + center.x; point.y = y2 + center.y;`

那应该改变每个点,从那里应该足够简单。中心是长度/ 2