2个二维数组相乘的方法
Method to multiply 2 two-dimensional array
我正在尝试创建一个方法,该方法将 2 个二维数组作为参数并打印它们的乘法数组(类似于方法中的矩阵乘法)。似乎它在运行时陷入了无限循环的某个地方。有什么修改吗?
public static void multiplicationOfArray(int[][] matrix1, int[][] matrix2) {
int row1 = matrix1.length;
int column1 = matrix1[0].length;
int row2 = matrix2.length;
int column2 = matrix2[0].length;
int[][] resultmatrix = new int[row1][column2];
if (column1 == row2) {
for (int i = 0; i < row1; row1++) {
for(int j = 0; j < column2; j++) {
for (int k = 0; k < column1; k++) {
resultmatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for (int row = 0; row < row1; row++) {
for (int column = 0; column < column2; column++) {
System.out.println(resultmatrix[row][column] + " ");
}
System.out.println();
}
}
}
你的 for 循环条件有点混乱。例如这个循环:
for (int i = 0; i < row1; row1++) {
只会在 i
大于 row1
时终止,但是,在剩余的代码或循环中,您不会增加 i
的值或减少 row1
,因此永远不会满足条件 i >= row1
。
试试这个:
public static void multiplicationOfArray(int[][] matrix1, int[][] matrix2) {
int row1 = matrix1.length;
int coloumn1 = matrix1[0].length;
int row2 = matrix2.length;
int coloumn2 = matrix2[0].length;
int[][] resultmatrix = new int[row1][coloumn2];
if (coloumn1 == row2) {
for (int i = 0; i < row1; i++) {
for(int j = 0; j < coloumn2; j++) {
for (int k = 0; k < coloumn1; k++) {
resultmatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for (int row = 0; row < row1; row++) {
for (int coloumn = 0; coloumn < coloumn2; coloumn++) {
System.out.println(resultmatrix[row][coloumn] + " ");
}
System.out.println();
}
}
}
如果尺寸不是 right/valid 那么您可能会抛出异常。
您也可以试试这个方法 -
public static double[][] multiply(double[][] A, double[][] B) {
int mA = A.length;
int nA = A[0].length;
int mB = B.length;
int nB = B[0].length;
if (nA != mB) throw new RuntimeException("Illegal matrix dimensions.");
double[][] C = new double[mA][nB];
for (int i = 0; i < mA; i++)
for (int j = 0; j < nB; j++)
for (int k = 0; k < nA; k++)
C[i][j] += A[i][k] * B[k][j];
return C;
}
可以找到完整的代码 here。
我正在尝试创建一个方法,该方法将 2 个二维数组作为参数并打印它们的乘法数组(类似于方法中的矩阵乘法)。似乎它在运行时陷入了无限循环的某个地方。有什么修改吗?
public static void multiplicationOfArray(int[][] matrix1, int[][] matrix2) {
int row1 = matrix1.length;
int column1 = matrix1[0].length;
int row2 = matrix2.length;
int column2 = matrix2[0].length;
int[][] resultmatrix = new int[row1][column2];
if (column1 == row2) {
for (int i = 0; i < row1; row1++) {
for(int j = 0; j < column2; j++) {
for (int k = 0; k < column1; k++) {
resultmatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for (int row = 0; row < row1; row++) {
for (int column = 0; column < column2; column++) {
System.out.println(resultmatrix[row][column] + " ");
}
System.out.println();
}
}
}
你的 for 循环条件有点混乱。例如这个循环:
for (int i = 0; i < row1; row1++) {
只会在 i
大于 row1
时终止,但是,在剩余的代码或循环中,您不会增加 i
的值或减少 row1
,因此永远不会满足条件 i >= row1
。
试试这个:
public static void multiplicationOfArray(int[][] matrix1, int[][] matrix2) {
int row1 = matrix1.length;
int coloumn1 = matrix1[0].length;
int row2 = matrix2.length;
int coloumn2 = matrix2[0].length;
int[][] resultmatrix = new int[row1][coloumn2];
if (coloumn1 == row2) {
for (int i = 0; i < row1; i++) {
for(int j = 0; j < coloumn2; j++) {
for (int k = 0; k < coloumn1; k++) {
resultmatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for (int row = 0; row < row1; row++) {
for (int coloumn = 0; coloumn < coloumn2; coloumn++) {
System.out.println(resultmatrix[row][coloumn] + " ");
}
System.out.println();
}
}
}
如果尺寸不是 right/valid 那么您可能会抛出异常。 您也可以试试这个方法 -
public static double[][] multiply(double[][] A, double[][] B) {
int mA = A.length;
int nA = A[0].length;
int mB = B.length;
int nB = B[0].length;
if (nA != mB) throw new RuntimeException("Illegal matrix dimensions.");
double[][] C = new double[mA][nB];
for (int i = 0; i < mA; i++)
for (int j = 0; j < nB; j++)
for (int k = 0; k < nA; k++)
C[i][j] += A[i][k] * B[k][j];
return C;
}
可以找到完整的代码 here。