在这种情况下如何将对象更改为数组?
How do I change an object to an array in this scenario?
我知道 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;
}
}
添加一个 getter getMatrix() 即 returns 矩阵;
在 matrixMultiply() 中使用 m1.getMatrix() 而不仅仅是 m1。
我知道 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;
}
}
添加一个 getter getMatrix() 即 returns 矩阵;
在 matrixMultiply() 中使用 m1.getMatrix() 而不仅仅是 m1。