Java資料結構:實現矩陣的乘積
阿新 • • 發佈:2019-01-30
public class 矩陣乘積 { public static void main(String[] args) { int array1[][] = new int[3][3]; int array2[][] = new int[3][3]; int chengArray[][] = new int[3][3]; int middleArray[][] = new int[3][3]; int tArray[][] = new int[3][3]; int n = 0; for (int i = 0; i < array1.length; i++) { for (int j = 0; j < array1[i].length; j++) { n++; array1[i][j] = n; array2[i][j] = n; } } for (int q = 0; q < 3; q++) { //兩個大迴圈控制乘機矩陣 for (int p = 0; p < 3; p++) { middleArray[q][p] = 0; //初始化中間矩陣 for (int j = 0; j < 3; j++) { middleArray[q][p] += array1[q][j] * array2[j][p]; //小迴圈控制各個乘機相加 //矩陣的第q行第p列=第一個矩陣的q行的 } //3個元素乘第二個矩陣的p列的三個元素 chengArray[q][p] = middleArray[q][p]; System.out.print(chengArray[q][p] + " "); //輸出 } System.out.println(); } } }