Arrays.binarySearch doesnt work like it should

萝らか妹 提交于 2019-12-20 05:15:57

问题


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?


回答1:


An array must be sorted.

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


来源:https://stackoverflow.com/questions/1356621/arrays-binarysearch-doesnt-work-like-it-should

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!