1

I want to hold the previous value after returning from a recursion. It worked for COUNT, but Array is not holding the previous value that I want.

The result is:

before recursion[0, 0, 0, 0]count: 0

before recursion[0, 1, 0, 0]count: 1

before recursion[0, 1, 2, 0]count: 2

before recursion[0, 1, 2, 3]count: 3

After recursion[0, 1, 2, 3]count: 3

After recursion[0, 1, 2, 3]count: 2

After recursion[0, 1, 2, 3]count: 1

After recursion[0, 1, 2, 3]count: 0

But the result i want is:

before recursion[0, 0, 0, 0]count: 0

before recursion[0, 1, 0, 0]count: 1

before recursion[0, 1, 2, 0]count: 2

before recursion[0, 1, 2, 3]count: 3

After recursion[0, 1, 2, 3]count: 3

After recursion[0, 1, 2, 0]count: 2

After recursion[0, 1, 0, 0]count: 1

After recursion[0, 0, 0, 0]count: 0

 import java.util.Arrays;

 import  java.util.Scanner;

 public class main {

     public static void boarder(int board[],int count)
     {

         if(count==4)
         {
             return;
         }
         board[count]=count;
         int temp=count+1;

         System.out.println("before recursion"+Arrays.toString(board)+"count: "+(count));
         boarder(board,temp);
         System.out.println("After recursion"+Arrays.toString(board)+"count: "+(count));

     }

     public static void main(String[] args)
     {
         int count=0;
         int board[]={0,0,0,0};
         //state tic=new state(board);
         boarder(board,0);
     }

 }
Abhishek
  • 3,348
  • 3
  • 15
  • 34
shovon_z
  • 11
  • 1
  • 1
    This is a typical referential problem. Remember that the `board` array is a single object being passed around by reference and manipulated in your regression call stack. If you want to print the state of the array when returning from your regression calls, you'll have to save a copy of the array in a local variable to print it out. – rafaelbattesti May 10 '18 at 04:02
  • 1
    By copying the array, I mean creating a new object (cloning), therefore saving the state of the array before the regression call. Something like this https://stackoverflow.com/q/14149733/2727717 – rafaelbattesti May 10 '18 at 04:17

4 Answers4

1

You should recover previous value like this.

public static void boarder(int board[], int count) {
    if (count == 4) {
        return;
    }
    int previous = board[count];
    board[count] = count;
    int temp = count + 1;
    System.out.println("before recursion" + Arrays.toString(board) + "count: " + (count));
    boarder(board, temp);
    System.out.println("After recursion" + Arrays.toString(board) + "count: " + (count));
    board[count] = previous;
}

result

before recursion[0, 0, 0, 0]count: 0
before recursion[0, 1, 0, 0]count: 1
before recursion[0, 1, 2, 0]count: 2
before recursion[0, 1, 2, 3]count: 3
After recursion[0, 1, 2, 3]count: 3
After recursion[0, 1, 2, 0]count: 2
After recursion[0, 1, 0, 0]count: 1
After recursion[0, 0, 0, 0]count: 0
0

Your code work as expected.

The final board[] is [0, 1, 2, 3]

You see the after strings are same because it's executed after the recursion complete, when your board[] = [0, 1, 2, 3] so they all print the same string

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
0

board is an array which is an object but count is a primitive variable. For primitives values are passed by value while for objects values are passed by reference. So the same object(same memory location) is passed through the methods for 'board'. So when you do a change to 'board', that change is reflected in all the places 'board' is referred. Primitive variables ('count' in this case) do not behave like this. There's a new memory location created each time they are passed through a method. When you do a change to the passed variable it does not change the original variable.

Imal
  • 481
  • 1
  • 6
  • 14
0

You need to read this

Change your boarder funtion to

board[count]=count;
int temp=count+1;
int[] tempArr = Arrays.copyOf(board, board.length);
System.out.println("before recursion"+Arrays.toString(board)+"count: "+(count));
boarder(tempArr,temp);
System.out.println("After recursion"+Arrays.toString(board)+"count: "+(count));
Anh N. Nguyen
  • 489
  • 1
  • 6
  • 19
  • 1
    If he does that, he'll be manipulating different copies of the array, and not the original `board` array itself. I would suggest printing the copy `tempArr` but not passing it into the regression call stack. – rafaelbattesti May 10 '18 at 04:31