MyMatrix矩阵实现(自娱自乐)

来源:互联网 发布:linux与ubuntu的区别 编辑:程序博客网 时间:2024/06/06 00:21
(MyMatrix.java)
/*
 * MyMatrix.java
 *
 * Created on 2006年11月20日, 上午7:53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.jiaoning.myDynamicProgramming;

import java.util.Random;

/**
 *
 * @author jiao
 */
public class MyMatrix {
   
    Random random = new Random();
    private int[][] matrix;
    public int row;
    public int column;
    public static final int ROW = 3;
    public static final int COLUMN = 3;
    public static final int RANGE = 10;
   
    /**
     * Creates a new instance of MyMatrix
     */
    public MyMatrix() {
        row = ROW;
        column = COLUMN;
        generateMatrix(row, column);
    }
   
    public MyMatrix(int r, int c) {
        row = r;
        column = c;
        generateMatrix(row, column);
    }
   
    public MyMatrix(int m[][]) {
        //注意!!row和column一定要设置
        row = m.length;
        column = m[0].length;
        matrix = m;
    }
   
    /*根据给定的行数和列数生成row×column的矩阵
     *矩阵元素的大小由RANGE确定(为0~9)
     */
    private void generateMatrix(int row, int column) {
        matrix = new int[row][column];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                matrix[i][j] = random.nextInt(RANGE);
            }
        }      
    }
   
     public int getElement(int i, int j) {
        return matrix[i][j];
    }
   
    private int[][] getMatrix() {
        int r = row;
        int c = column;
        int[][] matrix = new int[r][c];
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                matrix[i][j] = getElement(i, j);
            }
        }
        return matrix;
    }
   
    public MyMatrix multiply(MyMatrix m) {
        int[][] a = this.getMatrix();
        int[][] b = m.getMatrix();
        if (this.column != m.row) {
            System.out.println("两矩阵不可相乘!");
            System.exit(0);
        }
        int[][] result = new int[this.row][m.column];
        for (int i = 0; i < this.row; i++) {
            for (int j = 0; j < m.column; j++) {
                for (int k = 0; k < this.column; k++) {
                    result[i][j] += a[i][k] * b[k][j];
                }
            }
        }
        return new MyMatrix(result);
    }
   
    public void printMatrix() {
        for (int i = 0; i < row; i++) {
            System.out.print("/t");
            for (int j = 0; j < column; j++) {
                /*
                 *c语言风格的输出
                 *'%'指明了格式字符串的开始
                 *'1$'指明是第一个参数(本例中只有一个参数matrix[i][j],1$可省略)
                 *'-'指明输出左对齐(使用-时必须指明输出宽度)
                 *'5'指明输出宽度
                 *'d'指明输出十进制整型值
                 *' '(空格)指明如果输出的值宽度不够指定的值(5)时将用什么来填充
                 */
                System.out.printf("%1$-5d ", matrix[i][j]);
            }
            System.out.println();
        }
    }
 
}

(MyMatrixTest.java)
/*
 * MyMatrixTest.java
 *
 * Created on 2006年11月20日, 上午8:42
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.jiaoning.myDynamicProgramming;

/**
 *
 * @author jiao
 */
public class MyMatrixTest {
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MyMatrix m1 = new MyMatrix();
        System.out.println("矩阵1=");
        m1.printMatrix();
       
        MyMatrix m2 = new MyMatrix(m1.column, m1.row);
        System.out.println("矩阵2=");
        m2.printMatrix();
       
        MyMatrix result = m1.multiply(m2);
        System.out.println("矩阵1×矩阵2=");
        result.printMatrix();
    }
   
}  
原创粉丝点击