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)
Share:
11,085
user3412722
Author by

user3412722

Updated on June 14, 2022

Comments

  • user3412722
    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: enter image description here

    • Alexis C.
      Alexis C. over 9 years
      For which test cases?
    • takendarkk
      takendarkk over 9 years
      You don't need a parameter for the array's length. If you have the array then you have it's length.
    • Joel
      Joel over 9 years
      It looks correct at first sight. What's the failing test case?
    • Alexis C.
      Alexis C. over 9 years
      @Takendarkk And how would you recurse without this third parameter?
    • Joel
      Joel over 9 years
      @Takendarkk It's not the length actually, but the index to look at. Would worth renaming.
    • takendarkk
      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
      Dexter over 9 years
      are 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
      Joel over 9 years
      I just tested it, and it returns what is expected (true). No issue here.
    • Joel
      Joel over 9 years
      @Takendarkk the initial call uses the length indeed. Subsequent recursive calls use (length - n)
    • Alexis C.
      Alexis C. over 9 years
      Regarding your testcases results, this is certainly not the code that you posted which generates this outputs.
    • paxdiablo
      paxdiablo over 9 years
      If 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
      TomTom over 9 years
      Agree. Those test results are wrong. The code works fine. len iterates from the highest index down to the lowest.
  • Joel
    Joel over 9 years
    You're right, it seems to explain all test cases. Good one!
  • user3412722
    user3412722 over 9 years
    Hi, @luuksen, I made that improvement, but it still seems to give me the same results.
  • luuksen
    luuksen over 9 years
    Hey! 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
    Harshit Kuchhal over 3 years
    Check Number in Array using Recursion in Java