gpt4 book ai didi

java - 测试Java代码时"Cannot find symbol"

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:07 25 4
gpt4 key购买 nike

我已经开始编写一个类来对矩阵进行建模,编译器给了我以下消息:

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)”矩阵。

关于java - 测试Java代码时"Cannot find symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3327174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com