Collections. Binarysearch (list, K key) clarification Java
•
Java
In view of the following declaration, it is taken from this Oracle Java Tutorial and related to the binarysearch() method of class Collections:
Why does the return value of binarysearch () not only return a negative index, but not a negative index minus 1? (BOLD part quoted above)
In short: why (– (insertion point) – 1) and not just (– (insertion point))?
Thank you in advance
Solution
That's because – (insertion point) is not clear You will not be able to distinguish between:
>Items found in bit 0; > Item not found, insertion point 0
When using – (insertion point) – 1, the above two cases will result in different return values (0 and - 1)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码