Oneself is a novice, research Java data structure, see two n-order matrix multiplication of the topic, I tried to write a simple demo, where there is bad place, also hope that we have more suggestions, directly on the code:
Package com.shujujiegou01;
public class Juzhenchengfa {
Computes the multiplication of two n-order matrices
public static void Squaremult (int[][] A, int[][] B, int[][] c, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; J < N; j + +) {
C[I][J] = 0;
for (int k = 0; k < n; k++) {
C[I][J] + = a[i][k] * B[k][j];
}
}
}
}
Print matrix
public static void PrintArray (int a[][]) {
for (int k = 0; k < a.length; k++) {
for (int l = 0; l < a[k].length; l++) {
Print each element and the location of its coordinates
System.out.print (A[k][l] + "-" + "(" + K + "," + L + ")" + ",");
}
System.out.println ();
}
}
Matrix Column Interchange
public static void Swaprowcloumn (int a[][]) {
int num = a.length;
Is for the convenience of debugging, it is so defined
int e[][] = new Int[num][num];
for (int k = 0; k < num; k++) {
for (int l = 0, x = a[k].length; l < x; l++) {
Print each element and the location of its coordinates
E[K][L] = a[l][k];
System.out.print (E[k][l] + "-" + "(" + K + "," + L + ")" + ",");
}
System.out.println ();
}
}
Main function test
public static void Main (string[] args) {
TODO auto-generated Method Stub
int a[][] = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int b[][] = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int c[][] = new INT[3][3];
System.out.println ("Matrix A:");
PrintArray (a);
System.out.println ("A matrix after the interchange of rows and columns:");
Swaprowcloumn (a);
PrintArray (a);
System.out.println ("Matrix B:");
PrintArray (b);
System.out.println ("A matrix after the interchange of rows and columns:");
Swaprowcloumn (b);
PrintArray (b);
Call
Squaremult (A, B, C, 3);
Print
System.out.println ("Matrix C:");
PrintArray (c);
Results of the calculation after the exchange
SYSTEM.OUT.PRINTLN ("Swap rows and columns of Matrix C:");
Swaprowcloumn (c);
}
}
Attach the result of the operation:
Red parts, personal feelings have problems, called the row and column interchange method, it should be calculated that the C matrix is in accordance with the column after the exchange of a matrix and B matrix to calculate, should directly get the answer to the red box; Feel where there is a problem, but can't say!!!
Java computes the multiplication of two n-order matrices