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