Question

I keep receiving the error indicated below. I assume I'm not declaring it properly:

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
}
Was it helpful?

Solution

You don't have a constructor for SparseMatrix that takes a single int argument. Also, this.rows and this.cols are int values, not SparseMatrix fields. Also, you need to initialize the final field matrix in the constructor. You probably want this:

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

}

OTHER TIPS

matrix is final so needs to be declared either in its declaration or in the constructor. Remove the final and you should compile ok although you'll need to initialise matrix at some point if you want to use it.

Also rows and cols are meant to be ints but you are assigning SparseMatric objects

I suspect you want something like

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;
} 

and then you'll use rows and cols in the matrix somehow. With more info we can help more ;)

this.rows and this.col are of type int, yet you are attempting to instantiate them as SparseMatrix. It's also problematic that you unconditionally instantiate a SparseMatrix within a SparseMatrix. Think about when that cycle terminates...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top