user2958542
user2958542

Reputation: 341

Setting the value of a treemap within a treemap?

Please bear with me, I've been searching online all morning trying to find the proper syntax for a slight problem that's occurring. How would you set a treemap within a treemap?

The instance variable for the map is:

    private final TreeMap<Integer,TreeMap<Integer,Double>> matrix;

/**
 * Change the value at a given position.
 * If the position is not in the matrix (i.e., i is not between 0 and rows-1
 * and j is not between 0 and cols-1), then don't change anything.
 *
 * @param i The row.
 * @param j The column.
 * @param v The new value.
 */
public void set(int i, int j, double v) {
    if (matrix.containsKey(i) && matrix.containsValue(j) == true) {
        matrix.remove(j); // Is this needed?
        matrix.put(i<j,v>); // ERROR: not right syntax for this 
    }
} // end of set method

Upvotes: 1

Views: 2257

Answers (2)

Sage
Sage

Reputation: 15408

private final TreeMap<Integer,TreeMap<Integer,Double>> matrix;

You can not assign a value to an instance which is declared as final, other than, at the statement you are declaring it:

public final TreeMap<Integer,TreeMap<Integer,Double>> matrix  = new TreeMap<>();

Then you should be able to put, get TreeMap from the matrix as usual:

matrix.put(1, new TreeMap<Integer, Double>());
matrix.get(1).put(1, 1.23);

Upvotes: 0

mre
mre

Reputation: 44240

Is this what you're looking for?,

matrix.get(i).put(j, v);

Upvotes: 2

Related Questions