package test;
import java.util.Arrays;
/**
* The usage of the matrix.
*
* @author 前夜
*/
public class MatrixMultiplication {
/**
*********************
*The entrance of the program.
*
* @param args
*********************
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
matrixMultiplicationTest();
}// Of main
/**
*********************
* Matrix multiplication.
* The columns of the first matrix should be equal to the rows of the second one.
* 第一个矩阵的列应等于第二个矩阵的行。
*
* @param paraFirstMatrix The first matrix.
* @param paraSecondMatrix The second matrix.
* @return The result matrix.
*********************
*/
public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatrix) {
// Get the number of rows and columns.
int m = paraFirstMatrix.length;
int n = paraFirstMatrix[0].length;
int p = paraSecondMatrix[0].length;
// Step 1. Dimension check.
if (paraSecondMatrix.length != n) {
System.out.println("The two matrices cannot be multiplied");
return null;
} // Of if
// Step 2. Triple cycle 三重循环
int[][] resultMatrix = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
} // Of for k
} // Of for j
} // Of for i
return resultMatrix;
}// Of multiplication
/**
*********************
* Unit test for respective method.
*********************
*/
public static void matrixMultiplicationTest() {
int[][] tempFirstMatrix = new int[2][3];
for (int i = 0; i < tempFirstMatrix.length; i++) {
for (int j = 0; j < tempFirstMatrix[0].length; j++) {
tempFirstMatrix[i][j] = i + j;
} // Of for j
} // Of for i
//遍历数组(非List,例如:int [][] 类型等)可以直接调用Arrays.deepToString(数组名)可以直接输出结果
System.out.println("The first matrix is: \r\n" + Arrays.deepToString(tempFirstMatrix));
int[][] tempSecondMatrix = new int[3][2];
for (int i = 0; i < tempSecondMatrix.length; i++) {
for (int j = 0; j < tempSecondMatrix[0].length; j++) {
tempSecondMatrix[i][j] = i * 10 + j;
} // Of for j
} // Of for i
System.out.println("The second matrix is: \r\n" + Arrays.deepToString(tempSecondMatrix));
int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondMatrix);
System.out.println("The third matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
System.out.println("Trying to multiply the first matrix with itself.\r\n");
tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
}// Of class matrixMultiplication
}// Of class MatrixMultiplication
12-10
581
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-06
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-17
152
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-22
3998
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-27
772
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-12
357
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-23
423
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)