next up previous index
Next: The Comparable Interface Up: Order Previous: Binary search   Index

Generic methods

The purpose of the previous example was to point out that, as written, the contains method is limited to the search of integer arrays. If you want to search arrays of floats, you would have to replace int by float in the parameter list
public static boolean contains(float item, float[] array)
Apart from that, in this special case, everything would be the same. The reason is that the comparison operators < and > are overloaded, and apply both to int and float. On the other hand, if you wanted to search an array of strings, you would have to modify the code. The operators < and > do not apply to strings in Java. Instead you need to use the String.compareTo method, which returns 0 if the strings are equal, or a positive or negative integer depending on whether this string is later or earlier in the ordering than the other.



Subsections

Peter Williams 2005-06-07