shawn.bug 2008-11-08 10:03
浏览 186
已采纳

谁能帮忙解释下这几行是什么意思,谢谢。

public class Matrix {

int rows;

int cols;

double data[][];

public Matrix() {   
    this(0, 0);   
}   

public Matrix(int rows, int cols) {   
    [color=red]this(rows, cols, new double[rows][cols]);   //后面为什么要创建new double[rows][cols][/color] 
}   

public Matrix(int rows, int cols, double[][] data) {   
   [color=red] super();   //为什么要调用父类的构造方法[/color]        this.rows = rows;   
    this.cols = cols;   
    this.data = data;   
}   

public double getData(int row, int col) {   
    return data[row][col];   
}   

public void setData(int row, int col, double value) {   
    data[row][col] = value;   
}   

public double[][] multiply(double[][] m2) {   
    int m1rows = data.length;   
    int m1cols = data[0].length;   
    int m2rows = m2.length;   
    int m2cols = m2[0].length;   
    if (m1cols != m2rows)   
        throw new IllegalArgumentException("matrix doesn't match");   
    double[][] result = new double[m1rows][m2cols];   

    // multiply   
    for (int i = 0; i < m1rows; i++)   
        for (int j = 0; j < m2cols; j++)   
            for (int k = 0; k < m1cols; k++)   
                result[i][j] += data[i][k] * m2[k][j];   

    return result;   
}   

public String toString() {   
   [color=red] StringBuffer sb = new StringBuffer();   

    int rows = data.length;   
    int cols = data[0].length;   

    sb.append("array[" + rows + "][" + cols + "] = " + "\n");   
    for (int i = 0; i < rows; i++) {   

        for (int j = 0; j < cols; j++)   
            sb.append(" " + data[i][j] + " ");   
        sb.append("\n");   
    }   
    sb.append("\n");   

    return sb.toString();   [/color]
}   

public static void main(String[] argv) {   

    double x[][] = { { 3, 2, 3 }, { 5, 9, 8 }, };   
    Matrix m = new Matrix(3, 2, x);   

    double y[][] = { { 4, 7 }, { 9, 3 }, { 8, 1 }, };   

    double z[][] = m.multiply(y);   

    Matrix zm = new Matrix(z.length, z[0].length, z);   

    // print result   
    System.out.println(zm);   
}   

}

  • 写回答

3条回答 默认 最新

  • linpyi 2008-11-08 10:21
    关注

    [code="java"]
    this(rows, cols, new double[rows][cols]); //后面为什么要创建new double[rows][cols]
    这句话的意思就是调用该类的其他构造方法,你底下还有个构造方法,如果你有看JDK底层代码,里面有很多都有N种构造方法,一环镶嵌一环,这是设计的时候设计的
    public Matrix(int rows, int cols, double[][] data) {//也就是调用这句
    super(); //为什么要调用父类的构造方法 this.rows = rows;

    this.cols = cols;

    this.data = data;

    }

    至于为什么要调用父类,你这没有使用继承,应该没什么用

    public String toString() {

    StringBuffer sb = new StringBuffer();

        int rows = data.length;   
        int cols = data[0].length;   
    
        sb.append("array[" + rows + "][" + cols + "] = " + "\n");   
        for (int i = 0; i < rows; i++) {   
    
            for (int j = 0; j < cols; j++)   
                sb.append(" " + data[i][j] + " ");   
            sb.append("\n");   
        }   
        sb.append("\n");   
    
        return sb.toString();   
    }   
    

    toString 是从写方法,也就是如果你需要打印出该类,只要直接调用
    new Matrix();就OK了
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算