shovon_z
shovon_z

Reputation: 11

Array not holding the previous value in recursion(JAVA)

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

 }

Upvotes: 1

Views: 407

Answers (4)

user4910279
user4910279

Reputation:

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

Upvotes: 1

Anh N. Nguyen
Anh N. Nguyen

Reputation: 509

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

Upvotes: 0

Imal
Imal

Reputation: 581

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.

Upvotes: 0

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

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

Upvotes: 0

Related Questions