Recursion(All Indices)

Why can’t we write code int his way?

package Section_6;

import java.util.Arrays;

public class Recursion_All_Indices {

public static void main(String[] args) {
	int[] arr = new int[] { 3, 8, 1, 8, 8, 1, 7 };
	int[] Arr = all_Indices(arr, 0, 8, 0);
	display(Arr);
}

public static int[] all_Indices(int[] arr, int si, int d, int count) {
	if (si == arr.length) {
		int[] base = new int[count];
		return base;
	}
	int[] Arr = null;
	if (arr[si] == d) {
		Arr[count] = si;
		Arr = all_Indices(arr, si + 1, d, count + 1);
	} else if (arr[si] != d) {
		Arr = all_Indices(arr, si + 1, d, count);
	}
//	if (arr[si] == d) {
	//	
	//}
	return Arr;
}

public static void display(int[] arr) {
	int i;
	for (i = 0; i < arr.length; i++) {
		System.out.print(arr[i] + " ");
	}
}

}

@VinayakSingh11111 Is this code even working? If you are asking for an alternate approach then atleast give me a working code! Sometimes you have to see if tail recursion will be better choice or head recursion.

Just dry run this, will it work?

Here you are trying to access an index, but your array is initialized to null.
Yes you can also do this way also but you are going wrong.

If you want to do work while stack is building then refer this, i have corrected ya code!

If your query is clear close this doubt by marking it resolved and also resolve your previous open doubts!