简体   繁体   English

Java中Integer [] [](矩阵)的数组列表

[英]Arraylist of Integer[][] (matrix) in java

I defined an ArrayList of matrix ( Integer[][] ) in Java. 我在Java中定义了矩阵的ArrayListInteger[][] )。 When I add a new matrix into the ArrayList , it changes all of the variables to the last matrix. 当我将新矩阵添加到ArrayList ,它将所有变量更改为最后一个矩阵。

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. 每100ms调用一次求解方法。

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: 每次更改right()方法以创建一个新矩阵:

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;
}

When you do: 当您这样做时:

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

You create an object and make the variable matrix point to it. 您创建一个对象并使变量matrix指向它。 When you call right(matrix) , you pass the original object to the right() method, so it gets changed. 调用right(matrix) ,会将原始对象传递给right()方法,因此它会被更改。 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("--------");
    }
}

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. 从您的代码示例不能确认这是问题所在,因为您尚未发布right()方法的代码。 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. 这个问题说明了如何克隆二维数组 ,您需要这样做以避免重复将同一数组添加到列表中。

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

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