Java - 如何将方形数组(二维)向右旋转 90 度?
Java - how to rotate square array (two-dimensional) by 90 degree to the right?
那么,它应该如何运作。我输入 "n" 和 "m" 来制作分辨率为 n*m 的二维矩阵。
我需要将它向右旋转 90 度,使其看起来像在绘画中制作的图像:
我写了一些代码,但我真的不能让它工作 - 看起来很容易,但每次我尝试让它工作时,我都会 "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();
}
}
}
我到底做错了什么?
`double x1 = point.x - center.x;
双 y1 = point.y - center.y;
double x2 = x1 * Math.cos(角度) - y1 * Math.sin(角度));
双 y2 = x1 * Math.sin(角度) + y1 * Math.cos(角度));
point.x = x2 + center.x;
point.y = y2 + center.y;`
那应该变换每个点,从那里应该足够简单了。中心是 length/2
这是您的程序的正确版本,所有 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);
}
}
那么,它应该如何运作。我输入 "n" 和 "m" 来制作分辨率为 n*m 的二维矩阵。 我需要将它向右旋转 90 度,使其看起来像在绘画中制作的图像:
我写了一些代码,但我真的不能让它工作 - 看起来很容易,但每次我尝试让它工作时,我都会 "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();
}
}
}
我到底做错了什么?
`double x1 = point.x - center.x; 双 y1 = point.y - center.y;
double x2 = x1 * Math.cos(角度) - y1 * Math.sin(角度)); 双 y2 = x1 * Math.sin(角度) + y1 * Math.cos(角度));
point.x = x2 + center.x; point.y = y2 + center.y;`
那应该变换每个点,从那里应该足够简单了。中心是 length/2
这是您的程序的正确版本,所有 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);
}
}