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);
     }

 }
2
  • 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. Commented May 10, 2018 at 4: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 stackoverflow.com/q/14149733/2727717 Commented May 10, 2018 at 4:17

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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));

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.