java - 为什么这个数组会被修改?

标签 java algorithm clone backtracking maze

<分区>

我有一个名为 maze 的数组,理论上,它应该只在 updateMaze() 方法中进行修改。这是因为那是我要输出到控制台的最终结果。问题是当 tempMaze 被修改时 maze 也会被修改。这是不应该发生的。我的第一个想法是他们指向内存中的相同引用,但我检查了一下,这是错误的。我不得不提到我在初始化时使用了 clone() 来使它们的内容相似,我不确定这是否是个问题。 (尽管我认为我理解 clone() 是做什么的,但我还不够熟悉,无法知道它是否是问题所在。)我的代码:

public class ThreadTheMaze {
    ArrayList<Cell> result = new ArrayList<Cell>();

    private String[][] maze;
    private String[][] tempMaze;

    private int initRowPosition;
    private int initColPosition;

    private int amtOfRows;
    private int amtOfCols;

    public ThreadTheMaze(int initRow, int initCol){
        initRowPosition = initRow;
        initColPosition = initCol;
        result.add(new Cell(initRowPosition, initColPosition));
    }

    public void loadMaze(){
        try{
            Scanner in = new Scanner(new File("mazeData.txt"));
            while (in.hasNextLine()){
                amtOfCols = in.nextLine().length();
                amtOfRows++;
            }
            in.close();

            maze = new String[amtOfRows][amtOfCols];

            in = new Scanner(new File("mazeData.txt"));
            for (int r = 0; r < amtOfRows; r++){
                String line = in.nextLine();
                for (int c = 0; c < amtOfCols; c++){
                    maze[r][c] = line.substring(0,1);
                    line = line.substring(1);
                }
            }
            tempMaze = maze.clone();
        }catch (FileNotFoundException e){
            System.err.print(e);
        }
    }

    public void printMaze(){
        for (int r = 0; r < amtOfRows; r++){
            for (int c = 0; c < amtOfCols; c++){
                System.out.print(maze[r][c]);
            }
            System.out.println();
        }
    }

    public void updateMaze(){
        for (int i = 0; i < result.size(); i++){
            maze[result.get(i).getRow()][result.get(i).getColumn()] = "!";
        }
    }

    /**
     @return ArrayList of objects 'Cell' that are the solution to the maze. (Note: if no solution then returns empty ArrayList)
    */


    public void solve(Cell cell){
        tempMaze[cell.getRow()][cell.getColumn()] = "!";
        ArrayList<Cell> neighbors = getNeighbors(cell);

        if ((cell.getRow() == 0 || cell.getRow() == tempMaze.length-1) || (cell.getColumn() == 0 || cell.getColumn() == tempMaze[0].length-1)){
            return;
        }

        if ((cell.getColumn() == initColPosition && cell.getRow() == initRowPosition) && neighbors.size() < 1){
            return;
        }

        // If not in init position and has no neighbors then backtrack
        if ((cell.getColumn() != initColPosition || cell.getRow() != initRowPosition) && neighbors.size() < 1){
            result.remove(result.size()-1);
            solve(result.get(result.size()-1));
        }else if (neighbors.size() >= 1){ // If has neighbors then choose one and call the method again
            result.add(neighbors.get(0));
            solve(neighbors.get(0));
        }
    }

    /**
     @return ArrayList of objects 'Cell' that are empty and available to move to.
    */

    private ArrayList<Cell> getNeighbors(Cell cell){
        ArrayList<Cell> neighbors = new ArrayList<Cell>();
        int row = cell.getRow();
        int column = cell.getColumn();
        int[][] moveLocs = {{row-1, column}, {row+1, column}, {row, column+1}, {row, column-1}};
        for (int r = 0; r < moveLocs.length; r++){
            int tRow = moveLocs[r][0];
            int tCol = moveLocs[r][1];
            if (isValid(tRow, tCol)){
                Cell neighbor = new Cell(tRow, tCol);
                neighbors.add(neighbor);
            }
        }
        return neighbors;
    }

    public boolean isValid(int row, int col){
        if(row < 0 || row >= amtOfRows){
            return false;
        }
        if (col < 0 || col >= amtOfCols){
            return false;
        }
        if (!tempMaze[row][col].equals(" ")){
            return false;
        }
        return true;
    }

}

Cell 类是一个简单的类,带有一些简单的 get 和 set 方法。

我知道这不是向您展示我的问题的最简洁方式,但我真的找不到可能存在问题的地方。谢谢。

最佳答案

clone() 很浅。 这意味着以下内容:

        tempMaze = maze.clone();

只克隆二维数组的第一层。换句话说,您得到一个新数组包含与原始数组相同的String[] 引用

有关如何解决此问题的建议,请参阅 How do I do a deep copy of a 2d array in Java?

关于java - 为什么这个数组会被修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23916936/

相关文章:

java - 我可以使用 Appium - WebDriver 从笔记本电脑 (mac) 在真实设备 (iPhone) 上运行应用程序吗?

java - eclipse java 代码样式格式化程序 : prevent line wrapping for the special cases

java - Spring嵌入式数据库IllegalState错误

node.js - 排序时流式传输大数据

jquery - 使用JQuery的clone()作为文本框的问题

java - 克隆可变成员

java - 如何解决此错误 weka.core.UnassignedDatasetException : Instance doesn't have access to a dataset

javascript - 追加新文件

c# - 在大数据集中寻找重复

android-studio - Android APP 重复安装无法在原装的情况下安装