views:

216

answers:

1

I have string array [1, 2, 3] and i search for all of those numbers using Arrays.binarySearch, it find 1 and 2, but with 3 it returns -1. any idea why it works that way? what is better alternative to always working search in array/collection?

+6  A: 

An array must be sorted.

   String []ar={"1","2","3"};
   System.out.println(java.util.Arrays.binarySearch(ar,"3"));
adatapost