I am looking to find an index against a certain value in a Java array of type int.
How can I do this? Can I apply the binarySearch method to a non sorted array?
Below is what I tried, but some times it gives correct answer but most of the time incorrect. What am I doing wrong?
Arrays.binarySearch(array, value);
解决方案
Integer[] array = {1,2,3,4,5,6};
Arrays.asList(array).indexOf(4);
Note that this solution is threadsafe because it creates a new object of type List.
Also you don't want to invoke this in a loop or something like that since you would be creating a new object every time