I've been asked to create a public method that does nothing but to call another method that is recursive. The purpose of the second method is to search for a Int value inside an array.
So far I have this:
int[] array = {1, 2, 3, 7, 8, 11, 20, 30, 50, 100};
int cont = 0;
public int searchI(int x) {
searchR(x);
return x;
}
private void searchR(int y) {
if (cont < array.length) {
if (array[cont] == y) {
System.out.println(y);
} else {
searchR(cont++);
}
}
}
However, no matter what number I use, either one in the array or not, it always prints the value of y. I'm still new to recursion and not quite grasping the essence of it yet. That's probably why my method isn't working (besides that it is wrong). Any thoughts? And probably, help to grasp the term better.
cont++you would still need to passy, and incrementcont++separately, before callingsearchR(y)in yourelsecondition