Если текущий индекс содержит искомое число, вы должны вернуть этот индекс.
Только если этого не произойдет, вы должны сделать рекурсивный вызов для поиска номера в оставшейся части массив.
public static int firstIndex(int input[], int x,int i) {
if (i > input.length - 1) { // i is beyond the indices of the array,
// so the number is not found
return -1;
}
if(input[i]==x) { // current index is the first index that contains x
return i;
} else { // make a recursive call to find the first index of x in the rest of the array
return firstIndex(input,x,i+1);
}
}
, который также может быть записан как:
public static int firstIndex(int input[], int x,int i) {
if(i > input.length - 1) {
return -1;
}
return (input[i] == x) ? i : firstIndex(input, x, i+1);
}