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

时间:2014-05-28 16:19:27

标签: 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方法。

我知道这不是向你提出我的问题的最简洁的方式,但实际上,如果问题可能存在,我无法找到一个地方。感谢。

1 个答案:

答案 0 :(得分:3)

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

        tempMaze = maze.clone();

仅克隆2D阵列的第一级。换句话说,您将获得一个新的数组,其中包含与原始数组相同的String[]引用

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