我已经开始编写一个类来对矩阵进行建模,编译器给了我以下消息:
Matrix.java:4: cannot find symbolsymbol : constructor Matrix(int[][])location: class Matrix Matrix y = new Matrix(x);
This is the code that I was trying to compile:
public class Matrix<E> {
public static void main(String[] args) {
int[][] x = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
Matrix y = new Matrix(x);
System.out.println(y.getRows());
System.out.println(y.getColumns());
}
private E[][] matrix;
public Matrix(E[][] matrix) {this.matrix = matrix;}
public E[][] getMatrix() {return matrix;}
public int getRows(){return matrix.length;}
public int getColumns(){return matrix[0].length;}
}
所以,我的问题是,为什么我会收到此错误,我应该更改什么来解决此问题?
尝试这样:
public class Matrix<E> {
public static void main(String[] args) {
Integer [][] x = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0, 1, 0}};
Matrix<Integer> y = new Matrix<Integer>(x);
System.out.println(y.getRows());
System.out.println(y.getColumns());
System.out.println("before: " + y);
Integer [][] values = y.getMatrix();
values[0][0] = 10000;
System.out.println("after : " + y);
}
private E[][] matrix;
public Matrix(E[][] matrix) {this.matrix = matrix;}
public E[][] getMatrix() {return matrix;}
public int getRows(){return matrix.length;}
public int getColumns(){return matrix[0].length;}
public String toString()
{
StringBuilder builder = new StringBuilder(1024);
String newline = System.getProperty("line.separator");
builder.append('[');
for (E [] row : matrix)
{
builder.append('{');
for (E value : row)
{
builder.append(value).append(' ');
}
builder.append('}').append(newline);
}
builder.append(']');
return builder.toString();
}
}
在我的机器上编译并运行。
您需要考虑其他事情:封装以及何时“私有(private)”不是私有(private)的。查看对代码的修改,看看我如何修改您的“私有(private)”矩阵。
我是一名优秀的程序员,十分优秀!