Home > database >  Why do arrays in recursive function permanently change when array is recursively called? (Java)
Why do arrays in recursive function permanently change when array is recursively called? (Java)

Time:10-22

So the goal of this code is to recursively move through a 2d array with frogs on certain cells. A frog can jump over another frog and delete it. This code is supposed to see how many different ways you can do that to get it to one frog. But after one path is found, the original array is changed and it still only has a single frog from the previous path. How do I change this so that the original array isn't affected?

public void RecursiveFunc(Units[][] array) {
    
        for(int x = 0; x < array.length; x  ){
            for(int y = 0; y < array.length; y  ) {
                temp[x][y] = array[x][y];
            }
        }
        
        
        //make move on temp board and recursively call again
        if (CheckNumberFrogs(temp) == 1 && CheckLastID(temp) == getgoalLoc()) {
                numOfPlans  = 1;
        }
        
        
        else {
            
            for(int i= 0; i < temp.length; i  ){
                for(int j = 0; j < temp.length; j  ){
              
                    if (temp[i][j].Frog == true) {
                        //Every possible movement is done here and then recursively recalled
                        
                        if (i > 1) {
                        //can go left
                            if (temp[i-1][j].Frog == true && temp[i-2][j].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i-1][j].removeFrog();
                                temp[i-2][j].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                            }
                        }
                        
                        
                        
                        if (i < (temp.length - 2)) {
                        //can go right
                            if (temp[i 1][j].Frog == true && temp[i 2][j].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i 1][j].removeFrog();
                                temp[i 2][j].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                            }
                        }
                        
                        
                        
                        if (j > 1) {
                        //can go down
                            if (temp[i][j-1].Frog == true && temp[i][j-2].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i][j-1].removeFrog();
                                temp[i][j-2].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                            }
                        }
                        
                        
                        
                        if(j < (temp.length - 2)) {
                        //can go up
                            if (temp[i][j 1].Frog == true && temp[i][j 2].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i][j 1].removeFrog();
                                temp[i][j 2].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                            }
                        }
                        
                        
                        
                        if (i > 1 && j > 1) {
                            //can go left and down (diagonal)
                            if (temp[i-1][j-1].Frog == true && temp[i-2][j-2].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i-1][j-1].removeFrog();
                                temp[i-2][j-2].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                            }
                        }
                        
                        
                        
                        if (i < (temp.length - 2) && j > 1) {
                            //can go right and down (diagonal)
                            if (temp[i 1][j-1].Frog == true && temp[i 2][j-2].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i 1][j-1].removeFrog();
                                temp[i 2][j-2].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                            }
                        }
                        
                        
                        
                        if (i > 1 && j < (temp.length - 2)) {
                            //can go left and up (diagonal)
                            if (temp[i-1][j 1].Frog == true && temp[i-2][j 2].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i-1][j 1].removeFrog();
                                temp[i-2][j 2].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                            }
                        }
                        
                        
                        
                        if (i < (temp.length - 2) && j < (temp.length - 2)) {
                            //can go right and up (diagonal)
                            if (temp[i 1][j 1].Frog == true && temp[i 2][j 2].Frog == false) {
                                temp[i][j].removeFrog();
                                temp[i 1][j 1].removeFrog();
                                temp[i 2][j 2].setFrog();
                                RecursiveFunc(temp);
                                for(int x = 0; x < array.length; x  ){
                                    for(int y = 0; y < array.length; y  ) {
                                        temp[x][y] = array[x][y];
                                    }
                                }
                                
                            }
                        }
                        
                        //end
                    }
                }
            }
        }
    }

x

CodePudding user response:

You have to explicitly make a copy of the array and store it.

For a two-dimensional array, this means you need an entire loop to copy the array:

int[][] copy(int[][] array) {
  int[][] result = new int[array.length][];
  for (int i = 0; i < array.length; i  ) {
    result[i] = Arrays.copyOf(array[i], array[i].length);
  }
  return result;
}

As for why, remember that objects are arrays and study https://stackoverflow.com/a/40523/869736.

  • Related