Java-come ruotare l'array quadrato (bidimensionale) di 90 gradi a destra?
Quindi, come dovrebbe funzionare. I ingresso "n" e " m " per fare matrice bidimensionale con risoluzione n*m. Ho bisogno di ruotarlo di 90 gradi a destra per renderlo in qualche modo come quell'immagine fatta in paint:
Ho scritto del codice ma non riesco davvero a farlo funzionare - sembra che sia facile ma ogni volta che provo a farlo funzionare ottengo "outofboundsexception". Eccolo qui:
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();
}
}
}
Cosa sto facendo di sbagliato?
2 answers
Ecco la versione corretta del programma, con tutti gli indici for-loop fissi e la stampa spostata in un metodo separato.
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);
}
}
`doppio x1 = punto.centro X.X; doppio y1 = punto.centro Y.y;
Doppio x2 = x1 * Matematica.cos (angolo) - y1 * Matematica.peccato (angolo)); doppio y2 = x1 * Matematica.sin (angolo) + y1 * Matematica.cos (angolo));
Punto.x = x2 + centro.X; punto.y = y2 + centro.y; "
Che dovrebbe trasformare ogni punto, dovrebbe essere abbastanza semplice da lì. Il centro è la lunghezza / 2