开发者

Recursive isMember method with only two arguments!

I need to create a recursive Boolean method named isMemeber. The method should accept two arguments ONLY: an array and a value. The method should return true if the value is found in the array, or fals开发者_JAVA百科e if the value is not found in the array.

I think that the base case will be if the passed array is empty, but I need help with the recursive case:

public static boolean isMember(int[] array, int value)
{
    if(array.length==0){
        return false; 
    }else{
        return isMember(???);           
    }
}

Here is how it looks with position variable:

public static boolean isMember(int[] array, int value, int position)
{
    if (position > -1)
    {

        if (array[position] == value)
        {
            return true;
        }
        else
        {
            return isMember(array, value, position - 1);
        }
    }
    return false;

}


If you need to use recursion you can copy the array on each recursion. This is inefficent, but using recursion is inefficient compared with using a loop. e.g. Arrays.indexOf()

public static boolean isMember(int[] array, int value) {
    if(array.length == 0) return false; 
    if(array[0] == value) return true;
    int[] array2 = new int[array.length-1];
    System.arraycopy(array,1,array2,0,array2.length);
    return isMember(array2, value);           
}


There is a slight issue with your problem. If you are going to use recursion then each array element needs to have a subsey of elements otherwise whay do you passed to the recursive method? If this is not the casr and the case is as you stated then solving this problem with recursion isnot appropriate. Also you are missing the value comparison.


See the MSDN Array class. This looks like it is c#. Maybe try the Array.Find<T> method.

Update:
For Java, I'd recommend looking at Arrays (Java 2 Platform):

binarySearch

public static int binarySearch(int[] a, int key)

Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort method above) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
    a - the array to be searched.
    key - the value to be searched for. 
Returns:
    index of the search key, if it is contained in the list; otherwise,> (-(insertion point) - 1). 

The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. See Also: sort(int[])


If this is homework and they want it recursive, then maybe you should:

1 look for the middle value of the array and check if it matches. If it matches, return true

2 apply the function to the first half of the array. If it returns true, return true

3 apply the function to the second half of the aray. If it returns true, return true

4 return false

No code since it is homework.


EDIT: Is the array ordered?


I was just doing the question, and checking answers for alternative ways. Maybe this might be useful when you have to match names to String arrays.

public class Recursion {

    public static void main(String[] args) {
        String[] array = {"Tom", "Mary"};
        if(isMember(array,"John"))
            System.out.print("Found!");
        else
            System.out.println("Not Found!");

    }

    public static boolean isMember(String[] array, String name)
    {   
        int i = array.length;
        if(array.length == 0)
            return false;
        if(array[i - 1].equals(name))
            return true;
        else
        {
            String[] array2 = new String[array.length - 1];
            for(int b = 0; b< array.length -1; b++)
            {
                array2[b] = array[b];
            }
            return isMember(array2, name);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜