1.整数矩阵及其运算
1.1 getter和setter
在这一段代码中,出现了常见的getter,setter的方法, 目的是为了访问控制。在使用这种方法后,必须通过引用.方法来进行访问,并且也可以在方法里面写明限制的条件。
1.2 Exception
exception和error虽然同属于错误,但是存在很大区别。举个例子,exception,在路上开车的时候,如果遇见堵车堵死,我们或许可以选择走辅道进行通行,意味着还有方法来挽救,而error,就像在路上自己的车给撞报废了,没法动了,出现严重错误,而必须停止一样。
1.2.1 try-catch-finally
Exception实际上是指程序自身能去处理的异常,例如,空指针,数组下标越界,类型转换等异常。常见有tyr-catch-finally来实现捕捉异常。
其中try语句是指在这段范围内捕获异常,如果在运行过程中,产生异常时,就会跳过该条语句后面的代码。这些异常对象就由后面的catch进行相应的处理。
catch : 每个try至少要有一个catch语句,其作用是来处理产生异常的对象。
finally:不管是否发生异常,finally后面的语句都必须要进行执行。
1.2.2 throws
常见 throws+异常类型,在31天的代码中,常见throws语句出现,例如用于检测矩阵相乘时,矩阵行列大小的判断。如果一个方法(中的语句执行时)可能生成某种异常, 但是并不能确定如何处理这种异常, 则此方法应显示地声明抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理。在方法声明中用throws语句可以声明抛出异常的列表, throws后面的异常类型可以是方法中产生的异常类型, 也可以是它的父类。一旦方法体执行时,出现异常,仍会在异常代码处,生成一个异常类的对象,此对象满足throws后异常类型是,就会被抛出。异常代码后续的代码,就不在执行。31天的代码中,两种情况都出现了。
2.代码实现
package tree;
import java.util.Arrays;
public class IntMatrix {
/**
* The data
*/
int[][] data;
/**
*
* @param paraRows rows
* @param paraColumns the number of columns
*/
public IntMatrix(int paraRows, int paraColumns) {
data = new int[paraRows][paraColumns];
}// of the first constructor
/**
*
* @param paraMatrix the given matrix
*/
public IntMatrix(int[][] paraMatrix) {
data = new int[paraMatrix.length][paraMatrix[0].length];
// Copy elements.
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = paraMatrix[i][j];
} // of j
} // Of i
}// Of the second constructor
/**
* The third constructor. Construct a copy of the given matrix
*
* @param paraMatrix the given matrix
*/
public IntMatrix(IntMatrix paraMatrix) {
this(paraMatrix.getData());
}// Of the third constructor
/**
*
***********************************
* @Title: getIdentityMatrix
* @Description:
* @param: @param paraRows
* @param: @return
* @return: IntMatrix
***********************************
*/
public static IntMatrix getIdentityMatrix(int paraRows) {
IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);
for (int i = 0; i < paraRows; i++) {
// According to access control, resultMatrix.data can be visited
// directly.
resultMatrix.data[i][i] = 1;
} // Of for i
return resultMatrix;
}// Of getIdentityMatrix
/**
*
***********************************
*
* Overrides the method claimed in Object, the superclass of any class.
*
***********************************
*/
public String toString() {
return Arrays.deepToString(data);
}// Of toString
/**
*
***********************************
* @Title: getData
* @Description:
* @param: @return
* @return: int[][]
***********************************
*/
public int[][] getData() {
return data;
}// Of getData
/**
*
***********************************
* @Title: getRows
* @Description:
* @param: @return rows
* @return: int
***********************************
*/
public int getRows() {
return data.length;
}// Of getRows
/**
*
***********************************
* @Title: getColumns
* @Description:
* @param: @return data[0].length
* @return: int
***********************************
*/
public int getColumns() {
return data[0].length;
}// Of getColumns
/**
*
***********************************
* @Title: setValue
* @Description: Set one the value of one element
* @param: @param paraRow the row of element
* @param: @param paraColumn the column of element
* @param: @param paraValue the new value
* @return: void
***********************************
*/
public void setValue(int paraRow, int paraColumn, int paraValue) {
data[paraRow][paraColumn] = paraValue;
}// Of setValue
/**
*
***********************************
* @Title: getValue
* @Description: Get the value of one element
* @param: @param paraRow
* @param: @param paraColumn
* @param: @return
* @return: value
***********************************
*/
public int getValue(int paraRow, int paraColumn) {
return data[paraRow][paraColumn];
}// Of getValue
/**
*
***********************************
* @Title: add
* @Description: Add two matrices
* @param: @param paraMatrix
* @param: @throws Exception
* @return: void
***********************************
*/
public void add(IntMatrix paraMatrix) throws Exception {
// Step 1. Get the data of the given matrix.
int[][] tempData = paraMatrix.getData();
// Step 2. Size check.
if (data.length != tempData.length) {
throw new Exception(
"Cannot add matrices. Rows not match: " + data.length + " vs. " + tempData.length + ".");
} // Of if
if (data[0].length != tempData[0].length) {
throw new Exception(
"Cannot add matrices. Rows not match: " + data[0].length + " vs. " + tempData[0].length + ".");
} // Of if
// Step 3. Add to me.
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] += tempData[i][j];
} // Of for j
} // Of for i
}// Of add
/**
*
***********************************
* @Title: add
* @Description: Add two existing matrices
* @param: @param paraMatrix1 The first matrix
* @param: @param paraMatrix2 The second matrix
* @param: @return
* @param: @throws Exception
* @return: IntMatrix
***********************************
*/
public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
// Step 1. Clone the first matrix.
IntMatrix resultMatrix = new IntMatrix(paraMatrix1);
// Step 2. Add the second one.
resultMatrix.add(paraMatrix2);
return resultMatrix;
}// Of add
/**
*
***********************************
* @Title: multiply
* @Description: Multiply two existing matrices.
* @param: @param paraMatrix1 The first matrix
* @param: @param paraMatrix2 The second matrix
* @param: @return
* @param: @throws Exception
* @return: IntMatrix
***********************************
*/
public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
// Step 1. Check size.
int[][] tempData1 = paraMatrix1.getData();
int[][] tempData2 = paraMatrix2.getData();
if (tempData1[0].length != tempData2.length) {
throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. " + tempData2.length + ".");
} // Of if
// Step 2. Allocate space.
int[][] resultData = new int[tempData1.length][tempData2[0].length];
// Step 3. Multiply.
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];
} // Of for k
} // Of for j
} // Of for i
// Step 4. Construct the matrix object.
IntMatrix resultMatrix = new IntMatrix(resultData);
return resultMatrix;
}// Of multiply
/**
*
***********************************
* @Title: main
* @Description:
* @param: @param args
* @return: void
***********************************
*/
public static void main(String args[]) {
IntMatrix tempMatrix1 = new IntMatrix(3, 3);
tempMatrix1.setValue(0, 1, 1);
tempMatrix1.setValue(1, 0, 1);
tempMatrix1.setValue(1, 2, 1);
tempMatrix1.setValue(2, 1, 1);
System.out.println("The original matrix is: " + tempMatrix1);
IntMatrix tempMatrix2 = null;
try {
tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The square matrix is: " + tempMatrix2);
IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);
try {
tempMatrix3.add(tempMatrix1);
} catch (Exception ee) {
System.out.println(ee);
} // Of try
System.out.println("The connectivity matrix is: " + tempMatrix3);
}// Of main
}
总结
灵活使用Getter和Setter将会很方便的进行数据访问限制,用起来确实很方便。之前没关注过this的用法紧急查看了this的用法