2016-09-16 55 views
0

我正在寻找覆盖/替换数组列表中的值的方法,数组列表本身是散列表中的值。如何替换本身是散列图值的数组列表中的值

这是我们提供的一些代码。

public Equations(Double[][] matrix) { 
    // save input matrix into M -- it represents the equations 
    M = new HashMap<Integer,ArrayList<Double>>(); 
    for (int i=0;i<matrix.length;i++) { // step through rows of matrix 
     ArrayList<Double> L = new ArrayList<Double>(Arrays.asList(matrix[i])); 
     M.put(i,L); 
     } 
    } 

    // handy method to return a specific coefficient in row/column 
    public Double Element(int row, int column) { 
    return M.get(row).get(column); 
    } 

我们使用Hashmaps来执行高斯消除。我准备了我的Gaussian Elimination代码,用于查找该行的因子,将其应用于该行的第一列值,然后从该行之前的值中减去该值。得到的数字是我需要传递给hashmap和arraylist来替换刚刚被分解和减去的值。

有没有办法让我用数字替换这个数字?或者我需要构建一个全新的数组列表,并使用M.put(key,arraylist)替换旧的数组列表?

+0

只需将其替换为通常情况即可。除非数组列表是关键字,否则散列表是不相关的。 – EJP

回答

0
M.get(i).set(j, value); 

其中i->在hashmap中的arraylist的关键。 j->数组列表中元素的索引。 value->你想要在指定索引处更新的新值。