1

I defined an ArrayList of matrix (Integer[][]) in Java. When I add a new matrix into the ArrayList, it changes all of the variables to the last matrix.

I mean when I add

0 4 4 2
0 4 4 2
1 2 3 4
4 5 9 7

and then add

1 4 7 8
0 1 2 3
4 5 6 7
4 1 2 3

When I print the elements like that:

1 4 7 8
0 1 2 3
4 5 6 7
4 1 2 3
-------
1 4 7 8
0 1 2 3
4 5 6 7
4 1 2 3

So what should I do for this? this is my code :

private Integer matrix[][] = new Integer[4][4];

public Integer[][] right(Integer[][] M) {
        for (int k = 0; k < 4; k++)
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 3; j++) {
                    if (M[i][j] != 0 && M[i][j + 1] == 0) {
                        M[i][j + 1] += M[i][j];
                        M[i][j] = 0;
                        new_tile = true;
                    }
                }
        for (int i = 0; i < 4; i++)
            for (int j = 3; j > 0; j--) {
                if (M[i][j] == M[i][j - 1] && M[i][j] != 0 && M[i][j - 1] != 0) {
                    M[i][j] += M[i][j - 1];
                    M[i][j - 1] = 0;
                    new_tile = true;
                }
            }
        for (int k = 0; k < 4; k++)
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 3; j++) {
                    if (M[i][j] != 0 && M[i][j + 1] == 0) {
                        M[i][j + 1] += M[i][j];
                        M[i][j] = 0;
                        new_tile = true;
                    }
                }
        return M;
    }


static void printMatrix(Integer[][] matrix) {
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[0].length; j++) {
            System.out.print(matrix[i][j]);
        }
        System.out.println();
    }
}

/**
 * Print the elements in a matrix list
 */
static void printMatrices(ArrayList<Integer[][]> matrices) {
    for (Integer[][] matrix : matrices) {
        printMatrix(matrix);
        System.out.println("--------");
    }
}
public void solve() {

    Integer[][] temp = right(matrix);
    printMatrix(temp);
    if (new_tile)
        visited_nodes.add(temp);
    else {
        printMatrices(visited_nodes);
    }
    refresh();
}

    @Override
    public void actionPerformed(ActionEvent e) {

        solve();
        repaint();
    }

and the solve method called every 100ms.

ali mardani
  • 335
  • 1
  • 5
  • 13
  • 1
    Can you show more code? Just a guess, but I'd bet you are not adding a new array/matrix - rather you are adding temp, changing its values, and then adding the same instance again – copeg Mar 27 '15 at 16:07
  • thanks for your attention.but I change the variables of temp every time I add temp to arraylist. – ali mardani Mar 27 '15 at 16:12
  • 1
    How do you iterate over k? The double loop above will print the matrix, but you'll need to loop over values of k to get each matrix in the ArrayList. – nomis Mar 27 '15 at 16:14
  • Like I said above, can you show more code (preferably as an MCVE)? Neither how the array/matries are added, nor how you fully print out the contents (how does your code loop over k?) is completely evident from the current code. – copeg Mar 27 '15 at 16:16
  • all of this code is in a function that I call it every 100ms. and k will increase in this function. I was checking all of this possible mistakes. – ali mardani Mar 27 '15 at 16:16
  • dear copeg , I change the code in question, does it help? – ali mardani Mar 27 '15 at 16:21
  • Helps a little, but I still don't understand 1) how this method is called 2) what the right() method does 3) what the variable 'matrix' is. Please see http://stackoverflow.com/help/mcve – copeg Mar 27 '15 at 16:25
  • 1
    Also, show the actual output, including the k + "-----" statement. – nomis Mar 27 '15 at 16:28
  • this method calls in actionperformed method. right changes some element of matrix. matrix also is a Integer[][], that I defined it in my constructor – ali mardani Mar 27 '15 at 16:29
  • Once again: MCVE, unless you want folks to guess. You've gotten some pretty good ones so far which I guess is a start – copeg Mar 27 '15 at 16:42
  • The link is in my comment above. See http://stackoverflow.com/help/mcve – copeg Mar 27 '15 at 16:47
  • I added the right method . This is 2048 game, that I want to implement DFS method. and matrix for the first time is 0002 0004 4000 0200 plz help me – ali mardani Mar 27 '15 at 16:51

3 Answers3

1

You actually modify always the same instance of matrix.

Change the right() method for it to create a new matrix each time it is called:

public Integer[][] right(Integer[][] M) {
    Integer[][] newM = new Integer[4][4];

    // modify newM in your algo instead of M
    // Showing only the first bloc
    for (int k = 0; k < 4; k++)
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 3; j++) {
                if (M[i][j] != 0 && M[i][j + 1] == 0) {
                    newM[i][j + 1] = M[i][j + 1] + M[i][j];
                    newM[i][j] = 0;
                    new_tile = true;
                }
            }
    //...
    return newM;
}
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
0

When you do:

private Integer matrix[][] = new Integer[4][4];

You create an object and make the variable matrix point to it. When you call right(matrix), you pass the original object to the right() method, so it gets changed. Instead, you should pass a copy of the current matrix or create a new matrix, depending on what you need.

Breaking your code into smaller methods would make it easier to reason about. Something like this:

/**
 * Print a single matrix
 */
static void printMatrix(Integer[][] matrix) {
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[0].length; j++) {
            System.out.print(matrix[i][j]);
        }
        System.out.println();
    }
}

/**
 * Print the elements in a matrix list
 */
static void printMatrices(List<Integer[][]> matrices) {
    for (Integer[][] matrix : matrices) {
        printMatrix(matrix);
        System.out.println("--------");
    }
}
Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
0

Adding an element to a list adds a reference to that element. If the element is subsequently changed, the changed element will be seen in the list.

It cannot be confirmed from your code sample that this is the problem because you have not posted the code for the right() method. My suspicion is that it returns the same, altered matrix every time instead of a new matrix with different values.

This question shows how to clone a 2D array which you need to do to avoid repeatedly adding the same one to your list.

Community
  • 1
  • 1
Neil Masson
  • 2,609
  • 1
  • 15
  • 23