我有两个名为 2D int 数组的对象:originalMap 和rendMap,它们包含对应于益智游戏地图的数字。从线程类中,我从类调用 Puzzle.java 中获取映射数组并对其进行修改。
int [][] rendedMap = Puzzle.getRendedMap().getMap();
int [][] originalMap = Puzzle.getMapObj().getMap();
这是我的线程循环:
drawPuzzle(画布,地图);获取画布和数组并绘制地图。
while(running){
if(!holder.getSurface().isValid())
continue;
canvas = holder.lockCanvas();
if(runSolution && movements.size()>0){
executeSolution();
drawPuzzle(canvas, rendedMap);
if(finished){//checks if the execution finished
runSolution=false;
}
if(message.equals("No more actions") ||
message.equals("Out of bounds, try again!")){
System.out.println(message);
drawPuzzle(canvas, originalMap);
}
}else{
drawPuzzle(canvas, originalMap);
}
drawMovements(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
这是我修改数组的方法:
public void executeSolution(){
numOfPlanets = 2;
for(int x=0;x<rendedMap.length-1; x++){
for(int y=0;y<rendedMap[x].length-1; y++){
//Checks where the robot is and saves its initial position
if(rendedMap[x][y]>=7 && rendedMap[x][y]<=9){
initX=x; initY=y; initial=true;
System.out.println("Initial X: "+initX +" Y: "+initY );
direction = rendedMap[x][rendedMap[x].length-1];
System.out.println("Direction: "+direction );
}
}//end inner for
}//end for
if(movements.size()>0){
for(int i=0; i<movements.size();i++){
if(movements.get(i).getColour().equals("none")){
if(movements.get(i).getMotion().equals("straight")){
if(direction==RIGHT){
if(rendedMap[initX][initY+1]==0){
finished=true;
message = "Out of bounds, try again!";
return;
}else{
rendedMap[initX][initY+1]= rendedMap[initX][initY+1]+6;
rendedMap[initX][initY]=rendedMap[initX][initY]-6;
}
}
}//action if
}//color if
}//movements for loop
if(numOfPlanets > 0 ){
finished = true;
message ="No more actions";
}
}//movements if statement
}
我的问题是,当我出于某种原因渲染 renderMap 数组时 originalMap 也会发生变化,而当我试图拉回 originalMap 时,它不会变回来。我什至将它们打印出来并逐行检查,它们是相同的。
任何提示可能导致这种情况?我花了几天的时间来弄清楚。