Java: Recursively search an array for an integer given an array, integer, and array length
11,085
I am almost certain, that your method parameters are in the wrong order:
Your results hint that you switched the 2nd and 3rd parameter!
Maybe this
static boolean search(int[] findIn, int target, int len)
should actually be
static boolean search(int[] findIn, int len, int target)
Author by
user3412722
Updated on June 14, 2022Comments
-
user3412722 almost 2 years
I'm trying to write a recursive method that accepts an int array, number of elements in the array, and an integer, and returns whether the integer is present as an element in the array.I just can't figure out why I this isn't working for all my test cases. Any help would be much appreciated!
public static boolean search(int[] findIn, int target, int len){ if(len == 0){ return false; }else if(findIn[len-1] == target){ return true; }else{ return search(findIn, target, len-1); } }
Yes I realize there are better ways other than recursion to do this, but it is required that I do it this way.
My main method looks like this: I'm just hard-coding it for the time being:
int[] arr = {1}; System.out.println(search(arr,1,1));
Testcases:
-
Alexis C. over 9 yearsFor which test cases?
-
takendarkk over 9 yearsYou don't need a parameter for the array's length. If you have the array then you have it's length.
-
Joel over 9 yearsIt looks correct at first sight. What's the failing test case?
-
Alexis C. over 9 years@Takendarkk And how would you recurse without this third parameter?
-
Joel over 9 years@Takendarkk It's not the length actually, but the index to look at. Would worth renaming.
-
takendarkk over 9 years@Joel I do believe "number of elements in the array" is length. I don't see how it would be the index to look at. If that were true and the element to look for was at index 0, this method would never find it. I must really be misunderstanding something here.
-
Dexter over 9 yearsare you sure in your test-cases you are sending the correct value of len every-time ? why not send arr.length when you first call it, just to be sure
-
Joel over 9 yearsI just tested it, and it returns what is expected (true). No issue here.
-
Joel over 9 years@Takendarkk the initial call uses the length indeed. Subsequent recursive calls use (length - n)
-
Alexis C. over 9 yearsRegarding your testcases results, this is certainly not the code that you posted which generates this outputs.
-
paxdiablo over 9 yearsIf you're 'just hard coding it for the time being', where are those test results coming from? I've tested them all and your posted code works fine.
-
TomTom over 9 yearsAgree. Those test results are wrong. The code works fine. len iterates from the highest index down to the lowest.
-
-
Joel over 9 yearsYou're right, it seems to explain all test cases. Good one!
-
user3412722 over 9 yearsHi, @luuksen, I made that improvement, but it still seems to give me the same results.
-
luuksen over 9 yearsHey! This method signature definitely does not return the same results, when your method calls didn't change. (or those of your test application)
-
Harshit Kuchhal over 3 yearsCheck Number in Array using Recursion in Java