IntMatrix
public static IntMatrix add(IntMatrix m1, IntMatrix m2) {
int r1 = m1.matrix.length;
int c1 = m1.matrix[0].length;
int r2 = m2.matrix.length;
int c2 = m2.matrix[0].length;
if (r1 != r2) {
throw new RuntimeException("两个矩阵的行高都不相等!");
}
if (c1 != c2) {
throw new RuntimeException("两个矩阵的列宽都不相等!");
}
IntMatrix resultMatrix = new IntMatrix(r1, c1);
for (int i = 0; i < m1.matrix.length; i++) {
for (int j = 0; j < m2.matrix.length; j++) {
resultMatrix.matrix[i][j] = m1.matrix[i][j] + m2.matrix[i][j];
}
}
return resultMatrix;
}
public static IntMatrix multiply(IntMatrix m1, IntMatrix m2) {
if (m2.matrix[0].length != m1.matrix.length) {
throw new RuntimeException("第一个矩阵的列宽不等于第二个矩阵的行高!");
}
int r = m1.matrix.length;
int c = m2.matrix[0].length;
IntMatrix resultMatrix = new IntMatrix(r, c);
for (int i = 0; i < m1.matrix.length; i++) {
for (int j = 0; j < m2.matrix.length; j++) {
for (int k = 0; k < m1.matrix[0].length; k++) {
resultMatrix.matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
}
}
}
return resultMatrix;
}

import java.util.Arrays;
public class IntMatrix {
int[][] matrix;
public IntMatrix(int row, int col) {
matrix = new int[row][col];
}
public IntMatrix(IntMatrix paraMatrix) {
for (int i = 0; i < paraMatrix.matrix.length; i++) {
for (int j = 0; j < paraMatrix.matrix[0].length; j++) {
matrix[i][j] = paraMatrix.matrix[i][j];
}
}
}
public String toString() {
return Arrays.deepToString(matrix);
}
public void setValue(int row, int col, int value) {
matrix[row][col] = value;
}
public int getValue(int row, int col) {
return matrix[row][col];
}
public static IntMatrix add(IntMatrix m1, IntMatrix m2) {
int r1 = m1.matrix.length;
int c1 = m1.matrix[0].length;
int r2 = m2.matrix.length;
int c2 = m2.matrix[0].length;
if (r1 != r2) {
throw new RuntimeException("两个矩阵的行高都不相等!");
}
if (c1 != c2) {
throw new RuntimeException("两个矩阵的列宽都不相等!");
}
IntMatrix resultMatrix = new IntMatrix(r1, c1);
for (int i = 0; i < m1.matrix.length; i++) {
for (int j = 0; j < m2.matrix.length; j++) {
resultMatrix.matrix[i][j] = m1.matrix[i][j] + m2.matrix[i][j];
}
}
return resultMatrix;
}
public static IntMatrix multiply(IntMatrix m1, IntMatrix m2) {
if (m2.matrix[0].length != m1.matrix.length) {
throw new RuntimeException("第一个矩阵的列宽不等于第二个矩阵的行高!");
}
int r = m1.matrix.length;
int c = m2.matrix[0].length;
IntMatrix resultMatrix = new IntMatrix(r, c);
for (int i = 0; i < m1.matrix.length; i++) {
for (int j = 0; j < m2.matrix.length; j++) {
for (int k = 0; k < m1.matrix[0].length; k++) {
resultMatrix.matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
}
}
}
return resultMatrix;
}
}