在这种情况下,如何将对象更改为数组?

时间:2015-10-13 02:37:47

标签: java arrays object matrix-multiplication

我理解matrixMultiply()的问题会遇到问题,因为m1不是数组,而是一个对象。现在代码中肯定存在很多错误,但目前我还是专注于在继续之前修复此方法。可能有一个简单的解决方案,但它现在还没有打击我。

public class APMatrix {

    KeyboardReader reader = new KeyboardReader();
    private int[][] matrix;

    public APMatrix(int R, int C, boolean enter) {

        matrix = new int[R][C];

        if (enter) {
            for (int r = 0; r < matrix.length; r++) {
                for (int c = 0; c < matrix[r].length; c++) {
                    matrix[r][c] = reader.readInt("What value do you want in row " + (r + 1) + ", col " + (c + 1) + "? ");
                }
            }
        }
    }

    public APMatrix(int[][] copyMe) {
        for (int r = 0; r < copyMe.length; r++) {
            for (int c = 0; c < copyMe[r].length; c++) {
                matrix[r][c] = copyMe[r][c];
            }
        }
    }

    public int getRows() {
        return matrix.length;
    }

    public int getColumns() {
        return matrix[0].length;
    }

    public APMatrix matrixMultiply(APMatrix m1) {
        if (this[0].length == m1.length) {
            int[][] m2 = new int[this.length][m1[0].length];

            for (int outer = 0; outer < this.length; outer++) {
                for (int inner = 0; inner < m1[0].length; inner++) {
                    for (int all = 0; all < m1.length; all++) {
                        m2[outer][inner] += m1[all][inner];
                    }
                }
            }
            return m2;
        } else {
            return null;
        }
    }

    public String toString() {
        String retobj = "wow";
        return retobj;
    }
}

1 个答案:

答案 0 :(得分:0)

添加一个返回矩阵的getter getMatrix();

在matrixMultiply()中使用m1.getMatrix()而不是m1。