我正在做矩阵转置,下面的代码适用于 2x2 转置矩阵,但它不适用于 2x3 转置矩阵,请帮助我做错了什么。
例外:
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:2
package Sep20;
import java.util.Scanner;
public class TMatrix {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter the No of rows ");
int row = in.nextInt();
System.out.println("Enter the No of coloumn");
int col = in.nextInt();
int first[][]=new int[row][col];
int transpose[][]=new int[col][row];
System.out.println("Enter the matrix");
for (int i = 0; i < row; i++) {
for (int j = 0; j <col; j++) {
first[i][j]= in.nextInt();
}
}
for (int i = 0; i <row; i++) {
for (int j = 0; j <col; j++)
{
transpose[j][i]=first[i][j];
}
}
System.out.println("Transpose of entered matrix:-");
for (int i = 0; i <row; i++) {
for (int j = 0; j <col; j++) {
System.out.print(transpose[i][j]+"\t");
}
System.out.println();
}
}
}