gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:25 26 4
gpt4 key购买 nike

<分区>

我有一个名为 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 方法。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com