为啥要修改此数组? [复制]
Posted
技术标签:
【中文标题】为啥要修改此数组? [复制]【英文标题】:Why is this Array getting modified? [duplicate]为什么要修改此数组? [复制] 【发布时间】:2014-05-28 16:19:27 【问题描述】:我有一个名为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 方法。
我知道这不是向您提出我的问题的最简洁的方式,但我真的找不到问题可能存在的地方。谢谢。
【问题讨论】:
@amit 是数组的内置方法 【参考方案1】:clone()
很浅。这意味着以下内容:
tempMaze = maze.clone();
只克隆二维数组的第一层。换句话说,您将获得一个新数组包含与原始数组相同的String[]
引用。
有关如何解决此问题的建议,请参阅How do I do a deep copy of a 2d array in Java?
【讨论】:
以上是关于为啥要修改此数组? [复制]的主要内容,如果未能解决你的问题,请参考以下文章