java:使用泛型進行矩陣的加法與乘法

首先看一下預覽截圖:
在這裏插入圖片描述
下面的程序來自Y.Daniel Liang老師的《Java語言程序設計~進階篇》,稍有一點改動。話不多說,直接上代碼:

package num;

abstract class GenericMatrix<E extends Number>{  
    //將矩陣元素相加的抽象方法;
    protected abstract E add(E o1, E o2);
    //將矩陣的兩個元素相乘的抽象方法
    protected abstract E multiply(E o1, E o2);
    //定義零矩陣的抽象方法
    protected abstract E zero(); 
    
    //將兩個矩陣相加,用泛型E來表示類,所以方法是非靜態的;
    public E[][] addMatrix(E[][] matrix1, E[][] matrix2){  
        //檢查矩陣matrix1和矩陣matrix2的大小是否相等,matrix1.length爲行數,matrix1[0].length爲列數
        if ((matrix1.length != matrix2.length) || (matrix1[0].length != matrix2[0].length)){  
            throw new RuntimeException("The matrices do not have the same size!");  
        }
        //不能用泛型類型來創建數組,要用(E[][])來轉換才能創建數組;
        E[][] result = (E[][])new Number[matrix1.length][matrix1[0].length];  
        for(int i=0;i<result.length;i++) 
            for(int j=0;j<result[i].length;j++) {
                result[i][j]=add(matrix1[i][j],matrix2[i][j]);
            }
        return result;             
    }
    public E[][] multiplyMatrix(E[][] matrix1, E[][] matrix2){
        if (matrix1[0].length != matrix2.length){  
            throw new RuntimeException("The matrices do not have the same size!");  
        }
        E[][] result = (E[][])new Number[matrix1.length][matrix2[0].length];  
        for(int i=0;i<result.length;i++) {
            for(int j=0;j<result[0].length;j++) {
                result[i][j]=zero();
                for(int k=0;k<matrix1[0].length;k++) {
                    result[i][j]=add(result[i][j],multiply(matrix1[i][k],matrix2[k][j]));
                }
            }
        }
        return result;
    }
    public static void printResult(Number[][] m1,Number[][] m2,Number[][] m3,char op) {
        for(int i=0;i<m1.length;i++) {
            for(int j=0;j<m1[0].length;j++)
                System.out.print(" "+m1[i][j]);
            if(i==m1.length/2)
                System.out.print(" "+op+ " ");
            else
                System.out.print("   ");
            for(int j=0;j<m2.length;j++)
                System.out.print(" "+m2[i][j]);
            if(i==m1.length/2)
                System.out.print(" = ");
            else
                System.out.print("   ");
            for(int j=0;j<m3.length;j++)
                System.out.print(m3[i][j]+" ");
            System.out.println();
        }
    }
}
class IntegerMatrix extends GenericMatrix<Integer>{  
    //實現GenericMatrix中的add抽象方法
    protected Integer add(Integer o1, Integer o2 ){  
        return o1+o2;  
    }  
    //實現GenericMatrix中的mltiply抽象方法
    protected Integer multiply(Integer o1, Integer o2){  
        return o1*o2;  
    }  
    //實現GenericMatrix中的zero抽象方法
    protected Integer zero(){  
        return 0;  
    }  
}  
  
//Double類類繼承了GenericMatrix<?extends Number>中的;
class DoubleMatrix extends  GenericMatrix<Double>{  
    //實現GenericMatrix中的三個抽象方法; 
    protected Double add(Double d1,Double d2){  
        return d1+d2;  
    }  
    protected Double multiply(Double d1,Double d2){  
        return d1*d2;  
    }  
    protected Double zero(){  
        return 0.0;  
    }  
  
}  
  
public class GenericMatrixmain{  
    public static void main(String[] args){  
        //創建Integer數組
        Integer[][] m1 = new Integer[][]{{1,2,3},{4,5,6},{7,8,9}};  
        Integer[][] m2 = new Integer[][]{{1,5,1},{2,4,2},{0,0,0}};
        
        //創建IntegerMatrix實例;
        IntegerMatrix integerMatrix = new IntegerMatrix(); 
        
        System.out.println("m1+m2 is ");
        GenericMatrix.printResult(m1,m2,integerMatrix.addMatrix(m1,m2),'+');
 
        System.out.println("m1*m2 is ");
        GenericMatrix.printResult(m1,m2,integerMatrix.multiplyMatrix(m1,m2),'*');
        //創建Double數組
        Double[][] d1 = new Double[][]{{1.1, 1.2, 1.3}, {2.1, 2.1, 2.1}, {3.1, 3.1, 3.1}};
        Double[][] d2 = new Double[][]{{1.1, 2.1, 3.1}, {2.1, 2.1, 2.1}, {3.1, 3.1, 3.1}};
 
        DoubleMatrix doubleMatrix = new DoubleMatrix();
 
 
        System.out.println("d1+d2 is ");
        GenericMatrix.printResult(d1,d2,doubleMatrix.addMatrix(d1,d2),'+');
 
        System.out.println("d1*d2 is ");
        GenericMatrix.printResult(d1,d2,doubleMatrix.multiplyMatrix(d1,d2),'*'); 
    }  
}  
寫在後面:

最近幾天有點心力交瘁,害,不要在做一個任務之前想着放縱幾天,一放縱就不是幾天的問題,當你下定決心開始任務時,你發現放縱的這幾天,把身體搞的力不從心了。算是一個教訓了,靜水流深,加油!!!堅持下去!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章