简体   繁体   English

Java 3d ArrayList subMatrix修改会更改3d ArrayList的其他子矩阵

[英]Java 3d ArrayList subMatrix modification alters other subMatrices of the 3d ArrayList

I am relatively new to Java and am running into an issue. 我对Java比较陌生,并且遇到了问题。 I have a 3d ArrayList and I send an item of this ArrayList to a method where it is modified. 我有一个3d ArrayList ,并将此ArrayList的一项发送到修改它的方法中。 But this seems to alter other items of the 3d ArrayList . 但这似乎改变了3d ArrayList其他项。

So I create a matrix ArrayList<ArrayList<ArrayList<Integer>>> and print out each item. 因此,我创建了一个矩阵ArrayList<ArrayList<ArrayList<Integer>>>并打印出每个项目。

for(int i =0; i<solutionsMatrix.size(); i++)
{
    //System.out.println(i);
    System.out.println(solutionsMatrix.get(i));
}

I then send this 3d ArrayList to another method. 然后,我将此3d ArrayList发送到另一个方法。 Where each subMatrix (i) is modified. 每个子矩阵(i)被修改的地方。 But when doing this somehow the modification of a matrix (i) also alters other (non-i) matrices. 但是当以某种方式这样做时,矩阵(i)的修改也会改变其他(非i)矩阵。

private ArrayList<ArrayList<ArrayList<Integer>>> siftSolutions(ArrayList<ArrayList<ArrayList<Integer>>> solutionsMatrix)
{
    for(int n=0; n<solutionsMatrix.size();n++){
    //System.out.println(solutionsMatrix.get(n));
    ArrayList<ArrayList<Integer>> currentMatrix = mirrorMatrix(solutionsMatrix.get(n));
    System.out.println(currentMatrix);
}

The issue is that the printed out matrix (commented out portion in second code block) does not match the printed out matrix of the first code block. 问题在于,打印输出矩阵(第二代码块中的注释掉的部分)与第一代码块的打印矩阵不匹配。 I have fiddled with the code and determined that the method mirrorMatrix is somehow altering the 3d matrix solutionsMatrix . 我摆弄了代码,并确定mirrorMatrix方法正在以某种方式更改3d矩阵solutionsMatrix

public ArrayList<ArrayList<Integer>> mirrorMatrix(ArrayList<ArrayList<Integer>> unmirrored) {
    //every value of ij needs to equal ji, thus simply transmit all non-zeros
    ArrayList<ArrayList<Integer>> mirror = new ArrayList<ArrayList<Integer>>(unmirrored);
    for (int i = 0; i < mirror.size(); i++) {
        for (int j = 0; j < mirror.size(); j++) {
            if (mirror.get(i).get(j) != 0) {
                mirror.get(j).set(i, mirror.get(i).get(j));
            }
        }
    }
    return mirror;
}

I have tried searching for an answer online but I could not find an explanation (which may simply be a result that I do not know the proper terminology to describe my problem well). 我尝试过在线搜索答案,但找不到解释(这可能仅仅是因为我不知道正确地描述问题的正确术语所致)。 I hope someone can explain to me why this is occurring so I can resolve the issue and avoid it in the future. 我希望有人可以向我解释为什么会这样,以便我能够解决该问题并在将来避免它。

ArrayList<ArrayList<Integer>> mirror = new ArrayList<ArrayList<Integer>>(unmirrored);

This line does not make a deep copy of all the elements in it. 该行不会对其中的所有元素进行深入复制。 It does create a new instance of an ArrayList , but all its the elements are the same instances of ArrayList<Integer> as were present in the unmirrored instance. 它确实创建了一个ArrayList的新实例,但是其所有元素都是ArrayList<Integer>的相同实例,与未unmirrored实例中的实例相同。

So changing those elements with mirror.get(j).set(i, mirror.get(i).get(j)); 因此,使用mirror.get(j).set(i, mirror.get(i).get(j));更改这些元素mirror.get(j).set(i, mirror.get(i).get(j)); does change the same instance mirror.get(j) and unmirrored.get(j) . 确实更改了相同的实例mirror.get(j)unmirrored.get(j) Ie, mirror.get(j) == unmirrored.get(j) . mirror.get(j) == unmirrored.get(j)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM