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

the doc for method Arrays.binarySearch() is wrong

    XMLWordPrintable

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      the doc for method Arrays.binarySearch() is wrong, which says the return value is the index of the search key when the key was found, otherwise -(insertion point - 1); but actually, the return value is - (insertion point + 1) when the key was not found!

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      (1)construct a array, like <code>int[] array = new int[]{1, 2, 3, 5}; </code>
      (2)use the method Arrays.binarySearch(int[], int) to search the int value '4'
      (3)use return value to resolve the insertion point


      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;

      public class ArraysTest {

          public static void main(String[] args) {
              int[] array = new int[]{1,2,3,5};
              int target = 4;
              int retVal = Arrays.binarySearch(array, target);
              int insertionPoint = -retVal + 1;

              System.out.println(insertionPoint);
          }

      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            sswsharm swati sharma
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: