Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4301217

javadoc for java.util.Arrays.binarySearch(Object[],Object) is unclear

XMLWordPrintable

    • beta
    • sparc
    • solaris_2.5



      Name: akC97680 Date: 12/22/99



      The javadoc spec for java.util.Arrays.binarySearch(Object[], Object) states:
      ----------- begin javadoc -----------------

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

           Searches the specified array for the specified object using the binary
           search algorithm. The array must be sorted into ascending order according
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           to the natural ordering of its elements (as by Sort(Object[]), above) 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.

      -- skip

           Throws:
               ClassCastException - if the array contains elements that are not
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               mutually comparable (for example, strings and integers), or the search
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               key in not mutually comparable with the elements of the array.

      ----------- end javadoc -------------------

      The ambiguity shows up in the case of array containing mutually incomparable
      elements. For example, as javadoc says, consider case of search of an Integer
      object in the array containing strings and integers. On one hand, the array must
      be sorted prior to search, if it is not so, result of execution will be
      undefined. On other hand, this array cannot be sorted as it does contain
      incomparable elements: call to sort will throw ClassCastException. The problem
      is what one should expect from the binarySearch(Object[], Object) if tries to
      search Integer object in this array?

      Current implementation sometimes throws ClassCastException, sometimes does
      not (depending on elements' location and values).

      Also there is the JCK test, that fails because of this reason.
      api/java_util/Arrays/TestDesc.html#Arrays.binarySearch(Object[],Object)_fail

      ======================================================================

            shommel Scott Hommel (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: