-1

我不断收到下面指示的错误。我假设我没有正确声明它:

public class SparseMatrix {

// instance variables 
private final TreeMap<Integer,TreeMap<Integer,Double>> matrix;
private final int rows;
private final int cols;

public SparseMatrix(int r, int c) {
              // this gives me an error
    this.rows = new SparseMatrix(r);
    this.cols = new SparseMatrix(c);

} // end of constructor
}
4

3 回答 3

1

你没有一个构造函数,SparseMatrix它需要一个 int 参数。此外,this.rowsandthis.colsint值,而不是SparseMatrix字段。此外,您需要在构造函数中初始化该final字段matrix。你可能想要这个:

public class SparseMatrix {

    // instance variables 
    private final TreeMap<Integer,TreeMap<Integer,Double>> matrix;
    private final int rows;
    private final int cols;

    public SparseMatrix(int r, int c) {
        this.rows = r;
        this.cols = c;
        this.matrix = new TreeMap<>();
    } // end of constructor

}
于 2013-11-05T22:45:52.617 回答
0

矩阵是最终的,因此需要在其声明或构造函数中声明。删除final,你应该可以编译,尽管如果你想使用它,你需要在某个时候初始化矩阵。

行和列也应该是整数,但您正在分配 SparseMatric 对象

我怀疑你想要类似的东西

private TreeMap<Integer,TreeMap<Integer,Double>> matrix;
private final int rows;
private final int cols;

public SparseMatrix(int r, int c) {
  this.rows = r;
  this.cols = c;
} 

然后你会以某种方式在矩阵中使用行和列。有了更多信息,我们可以提供更多帮助;)

于 2013-11-05T22:44:03.157 回答
0

this.rows并且this.col属于类型 int,但您正试图将它们实例化为SparseMatrix. SparseMatrix在 a内无条件实例化 a 也是有问题的SparseMatrix。想想那个循环什么时候结束……

于 2013-11-05T22:46:31.450 回答