ArrayHelper.binarySearch

Searches the specified array for the specified object using the binary search algorithm. The array must be sorted into ascending order according to the specified comparator (as by the {@link #sort(Object[], Comparator) sort(T[], Comparator)} method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements equal to the specified object, there is no guarantee which one will be found.

@param <T> the class of the objects in the array @param a the array to be searched @param key the value to be searched for @param c the comparator by which the array is ordered. A {@code null} value indicates that the elements' {@linkplain Comparable natural ordering} should be used. @return index of the search key, if it is contained in the array; otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The <i>insertion point</i> is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or {@code a.length} if all elements in the array are less than the specified key. Note that this guarantees that the return value will be &gt;= 0 if and only if the key is found. @throws ClassCastException if the array contains elements that are not <i>mutually comparable</i> using the specified comparator, or the search key is not comparable to the elements of the array using this comparator.

  1. int binarySearch(T[] a, T key)
  2. int binarySearch(T[] a, int fromIndex, int toIndex, T key)
  3. int binarySearch(T[] a, T key, Comparator!T c)
    class ArrayHelper
    static
    int
    binarySearch
    (
    T
    )
    (
    T[] a
    ,
    T key
    ,)

Meta