user2173361 user2173361 - 2 months ago 7
Java Question

How to add the value of a array into a ArrayList in stead of the location of that array?

If you add a int[][] to a list, it will not add the value of the array, but the location of the array. How can I add the value of an array to a list. This is the code (This is just an example and might have small errors in it):

public class Main {
int[][] matrix = new int[2][2];
List<int[2][2]> matrixList = new ArrayList<>();

public static void main(String[] args) {
for(int i = 0; i < 4; i++){
matrixList.add(matrix);
matrix = matrixCalc(matrix);
}

for(int i = 0; i < 4; i++) {
System.out.println(Arrays.deepToString(matrixList.get(i)));
}
public int[][] matrixCalc(int[][] m){
//do various calculations with m
...

return m;
}
}
}


I want the output to have different matrixes that are calculated by the calculate method.

example of out put i want to get:

{{0,1}{5,7}}
{{2,0}{2,4}}
{{8,1}{4,8}}
{{3,3}{7,9}}


output I would get this way (WRONG!)

{{3,3}{7,9}}
{{3,3}{7,9}}
{{3,3}{7,9}}
{{3,3}{7,9}}

Answer

EDIT: After OP's comment I removed my entire answer (you can still find a screenshot of it here; or look in the history of the edits).

If I'm not mistaken, you send an empty Array to the method matrixCalc? For the calculations method I need a matrix with the same value as I just put on top of the stack.

In that case it's a bit different, although the problem is still having the same reference to matrix, so we'll have to change this. To copy the data from one 2D-array to a new one (with a new reference), we should make a separate copy-method. In Java there is the Arrays.copyOf-method (or alternatively System.arraycopy) for a 1D-array, but for a 2D-array you should make a method yourself. So your code becomes something like this:

public class Main {
    int[][] previousMatrix = new int[2][2];
    List<int[2][2]> matrixList = new ArrayList<>();

    public static void main(String[] args) {
        for(int i = 0; i < 4; i++){
            int[][] matrix = matrixCalc(previousMatrix);
            matrixList.add(matrix);
            previousMatrix = copy2dArray(matrix);
        }       

        for(int i = 0; i < 4; i++) {
            System.out.println(Arrays.deepToString(matrixList.get(i)));
        }
    }

    public int[][] matrixCalc(int[][] m){
        //do various calculations with m
        ...

        return m;
    }

    private int[][] copy2dArray(int[][] original){
        int[][] copy = new int[original.length][];
        for(int i = 0; i < original.length; i++){
            copy[i] = Arrays.copyOf(original[i], original[i].length);
        }
        return copy;
    }
}

Alternatively, you could make a new 2D array at the start of your matrixCalc method and fill and return that, instead of the parameter given. But, since I didn't knew what kind of calculations you are doing in your matrixCalc method I can't really give an example of this. It will look something like this:

public int[][] matrixCalc(int[][] m){
    int[][] n = new int[2][2];
    //do various calculations with m,
    //but save the result in n
    ...

    return n;
}