0

In java say, I have:

import java.util.*;
public class HelloWorld {
  public static void main(String[] args) {
  int[] arr = {1,1};
  int index = Arrays.binarySearch(arr, 1);
    System.out.println(index);
  }
}

This will always give the first index it finds, is there a way I can get the second one?

Onedayanam
  • 95
  • 7
  • 2
    what makes you think it will always return the first index? that's not what the [Arrays.binarySearch doc](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(int[],%20int)) says. in any case, binary search requires that the array must be sorted, so if there are multiple items with the same value, they will be at adjacent locations in the array. – jdigital Feb 19 '17 at 22:22
  • Hmm, the official JavaDoc says: '_If the array contains multiple elements with the specified value, there is no guarantee which one will be found._' The exact formulations may vary depending on the exact method used but the general idea stays the same. For reference see here [Java Arrays](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html). – Marco N. Feb 19 '17 at 22:24
  • There is a way to get all of them -> [all occurences of an object in Arraylist, in java](http://stackoverflow.com/questions/13900585/trying-to-find-all-occurances-of-an-object-in-arraylist-in-java) –  Feb 19 '17 at 22:28

0 Answers0