Collections.binarySearch(List list, K key) clarification. Java

烂漫一生 提交于 2019-11-28 11:26:33

That's because -(insertion point) would be ambiguous. You wouldn't be able to tell the following apart:

  • item found at position 0;
  • item not found, and insertion point is 0.

With -(insertion point) - 1, the above two cases result in different return values (0 and -1).

from your link

This admittedly ugly formula guarantees that the return value will be >= 0 if and only if the search key is found. It's basically a hack to combine a boolean (found) and an integer (index) into a single int return value

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