6

I have seen examples on the site that deal with generics with multiple parameters but none that work for my situation.

So here is the deal: I am trying to learn Java generics and have decided to create a simple binary array search utility function. I am testing it out using custom objects and integers. To get feedback on errors and warning I am using Eclipse. Here is what I have:

public static int binarySearch(Comparable[] array, Comparable item, int start, int end) {
    if(end < start) {
        return -1;
    }
    int mid = (start + end) / 2;
    if(item.compareTo(array[mid]) > 0) {
        return binarySearch(array, item, mid + 1, end);
    } else if(item.compareTo(array[mid]) < 0) {
        return binarySearch(array, item, start, mid - 1);
    } else {
        return mid;
    }
}

So obviously I get the warnings for Raw types saying the generics should be parameterized. How can I do this correctly given that I have multiple parameters which both need to be the same type?

SOLUTION

Here is the working solution using generics with the correct parameter checks:

public static <T extends Comparable<? super T>> int binarySearch(T[] array, T item, int start, int end) {
    if(array.length == 0) {
        return -1;
    }
    if(item == null) {
        return -1;
    }
    if(start < 0) {
        return -1;
    }
    if(end < start) {
        return -1;
    }
    int mid = (start + end) / 2;
    if(item.compareTo(array[mid]) > 0) {
        return binarySearch(array, item, mid + 1, end);
    } else if(item.compareTo(array[mid]) < 0) {
        return binarySearch(array, item, start, mid - 1);
    } else {
        return mid;
    }
}
2
  • Just letting you know, this will fail if you pass in a zero-length or null array. Commented May 6, 2012 at 23:05
  • Yep, it is a trivial example - I am interested in the concept of multiple generics not the binary search. Thanks though. Commented May 6, 2012 at 23:06

2 Answers 2

9

you can specify function-specific generic parameter like so

public static <T extends Comparable<? super T>> int binarySearch(T[] arr,T elem,int start,int end){
    //...
}
Sign up to request clarification or add additional context in comments.

4 Comments

what does the <? super T> do for this? Just curious is all - I see it often but never with an explanation.
it's some extra type safety and allows for more flexibility so you can pass an array of something whose superclass implements Comparable
@ratchetfreak Not exactly... It's saying that T can be a type of things that are comparable to some superclass of T, while forcing arr and elm to still be an array and an element of the same type.
@trutheality: What rathetfreak said is the usual case of when this happens. People usually write classes that are comparable to themselves. Thus if T is comparable to a superclass of T, that is usually because that superclass of T is comparable to itself, and T simply inherited it.
5

This is the typical way to create generic functions:

public static <T extends Comparable<? super T>> int binarySearch(T[] array, T item, int start, int end) { ... }

For extra generality, item doesn't have to be of the same type the things in the array are, and things in the array don't have to be Comparable since you're not comparing them to anything, so

public static <T> int binarySearch(T[] array, Comparable<T> item, int start, int end) { ... }

gives some additional flexibility.

2 Comments

nice! i was wondering that too. for the context i think a stricter approach would be better, but that is great to know. thanks.
great answer but just beat to the punch, i'll def 1 up your answer though, thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.