矩阵类的实现22.09.29
东北大学Java实验课,早八实验课的痛谁懂!
class Matrix {
private int[][] matrixArr;
/**
* 构造函数,传入一个数组克隆给矩阵类的矩阵数组。
* @param arr
*/
public Matrix(int[][] arr){
this.matrixArr = arr.clone();
}
/**
* 判断矩阵是否为空
* @return boolean
*/
public boolean isEmpty(){
if(matrixArr.length == 0 || matrixArr[0].length == 0){
return true;
}
return false;
}
/**
* 获取二维矩阵的行数和列数
* @return
*/
public int getMatrix_row(){
if(isEmpty() == true){
return 0;
}
return matrixArr.length;
}
public int getMatrix_column(){
if(isEmpty() == true){
return 0;
}
return matrixArr[0].length;
}
/**
* 二维矩阵的get和set方法
* @return
*/
public int[][] getMatrixArr(){
return matrixArr;
}
public void setMatrixArr(int[][] mat){
this.matrixArr = mat.clone();
}
/**
*从键盘读入一串数字赋给矩阵的二维数组
* @param
*/
public void readMatrix() {
if(isEmpty()) {
System.out.println("the matrix is null, can't calculate,it's meaningless!");
}
int[][] arr = new int[getMatrix_row()][getMatrix_column()];
Scanner scanner = new Scanner(System.in);
for(int i = 0; i < getMatrix_row(); i++) {
for(int j = 0; j < getMatrix_column(); j++) {
arr[i][j] = scanner.nextInt();
}
}
setMatrixArr(arr);
}
/**
* 矩阵的加法
* @param matrix
* @return Matrix型矩阵
*/
public Matrix matrixAdd(Matrix matrix) {
if (isEmpty() || matrix.isEmpty()) {
return null;
}
Matrix resultMatrix = new Matrix(new int[getMatrix_row()][getMatrix_column()]);
// int[][] result = new int[getMatrix_row()][getMatrix_column()];
for (int i = 0; i < getMatrix_row(); i++) {
for (int j = 0; j < getMatrix_column(); j++) {
resultMatrix.getMatrixArr()[i][j] = getMatrixArr()[i][j] + matrix.getMatrixArr()[i][j];
}
}
return resultMatrix;
}
/**
* 矩阵的乘法
* @param matrix
* @return Matrix型矩阵
*/
public Matrix matrixMultiply(Matrix matrix){
if(isEmpty() || matrix.isEmpty() || getMatrix_column() != matrix.getMatrix_row()) {
return null;
}
int length1 = getMatrix_row();
int length2 = matrix.getMatrix_column();
int length3 = getMatrix_column();
Matrix resultMatrix = new Matrix(new int[getMatrix_row()][matrix.getMatrix_column()]);
// int[][] result = new int[length1][length2];
for(int i = 0; i < length1; i++) {
for(int j = 0; j < length2; j++) {
for(int k = 0; k < length3; k++) {
resultMatrix.getMatrixArr()[i][j] += getMatrixArr()[i][k] * matrix.getMatrixArr()[k][j];
}
}
}
return resultMatrix;
}
/**
* 打印格式化好了的矩阵,
* @return
*/
public void printMatrix(){
for(int[] i: matrixArr){
System.out.print("[");
for(int j: i){
System.out.print(j + "\t");
}
System.out.println("]");
}
}
}
下面就是实现代码,给了三个矩阵,计算他们的加法和乘法的结果
public class Yu {
public static void main(String[] args) {
int[][] arr1 = new int[2][4];
int[][] arr2 = new int[4][2];
int[][] arr3 = new int[2][4];
Matrix matrix1 = new Matrix(arr1);
Matrix matrix2 = new Matrix(arr2);
Matrix matrix3 = new Matrix(arr3);
System.out.println("从键盘读入8个整数赋给arr1(以回车键结束):");
matrix1.readMatrix();
matrix1.printMatrix();
System.out.println("从键盘读入8个整数赋给arr2(以回车键结束):");
matrix2.readMatrix();
matrix2.printMatrix();
System.out.println("从键盘读入8个整数赋给arr3(以回车键结束):");
matrix3.readMatrix();
matrix3.printMatrix();
System.out.println("arr1和arr2相乘的结果为:");
matrix1.matrixMultiply(matrix2).printMatrix();
System.out.println("arr1和arr3相加的结果为:");
matrix1.matrixAdd(matrix3).printMatrix();
// Arrays.toString(null);
}
}