packagecom.one;importjava.util.Arrays;publicclassMatrixMultiplication{publicstaticvoidmain(String args[]){matrixMultiplicationTest();}// of mainpublicstaticint[][]multiplication(int[][] paraFirstMatrix,int[][] paraSecondMatrix ){int m = paraFirstMatrix.length;int n = paraFirstMatrix[0].length;int p = paraSecondMatrix[0].length;//如果第一个数组的列不等于第二个数组的行,则两个数组不能实现相乘if(paraSecondMatrix.length != n){System.out.println("The two matrices cannot be multiplied.");returnnull;}//实现相乘,并将数据存储在resultMatrixint[][] resultMatrix =newint[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 k}// of j}// of ireturn resultMatrix;}// of multiplicationpublicstaticvoidmatrixMultiplicationTest(){int[][] tempFirstMatrix =newint[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 j}// of iSystem.out.println("The first matrix is: \r\n"+Arrays.deepToString(tempFirstMatrix));int[][] tempSecondMatrix =newint[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 j}// of iSystem.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 matrixMultiplicationTest}// of class
矩阵相乘package com.one;import java.util.Arrays;public class MatrixMultiplication { public static void main(String args[]) { matrixMultiplicationTest(); }// of main public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMat