Given the following statement, taken from this Oracle java tutorial, related to the binarySearch() method of the class Collections:
The return value is the same for both forms. If the List contains the
search key, its index is returned. If not, the return value is
(-(insertion point) - 1), where the insertion point is the point at
which the value would be inserted into the List, or the index of the
first element greater than the value or list.size() if all elements in
the List are less than the specified value.
Why does the return value of binarySearch()
not return only the negative index instead of the negative index minus 1? (the part in bold of the quote above mentioned).
In brief: why (-(insertion point) - 1)
and not only (-(insertion point))
?
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…