1,矩阵对象的创建。
2,getRows等:getter,setter在java里面很常用,主要是为了访问控制。
3,整数矩阵的加法、乘法。
4,Exception的抛出与捕获机制。
5,用this调用其它的构造方法以减少冗余代码。
6,代码看起来多,但矩阵运算我们以前写过。
7,把数据类型修改成double,获得DoubleMatrix.java,以后会很有用。
8,getIdentityMatrix:单位矩阵。
9,resultMatrix.data[i][i]:成员变量的访问权限:在同一类里面是可以直接使用的。
package java31to40;
import java.util.Arrays;
public class D31_IntMatrix {
int[][] data;
public static void main(String[] args) {
D31_IntMatrix tempMatrix1 = new D31_IntMatrix(3, 3);
tempMatrix1.setValue(0, 1, 1);// 0行,1列,值1
tempMatrix1.setValue(1, 0, 1);// 1行,0列,值1
tempMatrix1.setValue(1, 2, 1);// 1行,2列,值1
tempMatrix1.setValue(2, 1, 1);// 2行,1列,值1
tempMatrix1.setValue(2, 2, 2);// 2行,2列,值2
System.out.println("原矩阵是: " + tempMatrix1);
D31_IntMatrix tempMatrix2 = null;
try {
tempMatrix2 = D31_IntMatrix.multiply(tempMatrix1, tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
}
System.out.println("方阵是: " + tempMatrix2);
D31_IntMatrix tempMatrix3 = new D31_IntMatrix(tempMatrix2);
try {
tempMatrix3.add(tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
}
System.out.println("连通性矩阵: " + tempMatrix3);
}
public D31_IntMatrix(int rows, int columns) {
data = new int[rows][columns];
}
public D31_IntMatrix(int[][] matrix) {
data = new int[matrix.length][matrix[0].length];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = matrix[i][j];
}
}
}
public D31_IntMatrix(D31_IntMatrix matrix) {
this(matrix.getData());
}
public static D31_IntMatrix getIdentityMatrix(int rows) {
D31_IntMatrix resultMatrix = new D31_IntMatrix(rows, rows);
for (int i = 0; i < rows; i++) {
resultMatrix.data[i][i] = 1;
}
return resultMatrix;
}
public String toString() {
return Arrays.deepToString(data);
}
public int[][] getData() {
return data;
}
public int getRows() {
return data.length;
}
public int getColumns() {
return data[0].length;
}
public void setValue(int row, int column, int value) {
data[row][column] = value;
}
public int getValue(int row, int column) {
return data[row][column];
}
public void add(D31_IntMatrix matrix) throws Exception {
int[][] tempData = matrix.getData();
if (data.length != tempData.length) {
throw new Exception("矩阵无法相加,行不匹配: " + data.length + " vs. " + tempData.length + ".");
}
if (data[0].length != tempData[0].length) {
throw new Exception("矩阵无法相加,列不匹配: " + data[0].length + " vs. " + tempData[0].length + ".");
}
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] += tempData[i][j];
}
}
}
public static D31_IntMatrix add(D31_IntMatrix matrix1, D31_IntMatrix matrix2) throws Exception {
D31_IntMatrix resultMatrix = new D31_IntMatrix(matrix1);
resultMatrix.add(matrix2);
return resultMatrix;
}
public static D31_IntMatrix multiply(D31_IntMatrix matrix1, D31_IntMatrix matrix2) throws Exception {
int[][] tempData1 = matrix1.getData();
int[][] tempData2 = matrix2.getData();
if (tempData1[0].length != tempData2.length) {
throw new Exception("矩阵无法相乘: " + tempData1[0].length + " vs. " + tempData2.length + ".");
}
int[][] resultData = new int[tempData1.length][tempData2[0].length];
for (int i = 0; i < tempData1.length; i++) {
for (int j = 0; j < tempData2[0].length; j++) {
for (int k = 0; k < tempData1[0].length; k++) {
resultData[i][j] += tempData1[i][k] * tempData2[k][j];
}
}
}
D31_IntMatrix resultMatrix = new D31_IntMatrix(resultData);
return resultMatrix;
}
}
结果输出:
原矩阵是: [[0, 1, 0], [1, 0, 1], [0, 1, 2]]
方阵是: [[1, 0, 1], [0, 2, 2], [1, 2, 5]]
连通性矩阵: [[1, 1, 1], [1, 2, 3], [1, 3, 7]]