473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

ArrayList.BinarySearch returns various negative values if item not found

So I see from the documentation here:
http://msdn.microsoft.com/library/de...earchTopic.asp

That the code uses the less than operator:
int myIndex=myList.BinarySearch( myObject );
if ( myIndex < 0 )

when using the BinarySearch method and in my own experience I see that
sometimes it returns other negative values (other than -1).

What is the significance of the various negative values? I'm used to
methods that return indices to return only -1 on failure - do these
other negative values tell you how many times it had to divide and
conquer to reach the conclusion that the value doesn't occur in the
ArrayList?

Thanks,
Novice

Apr 18 '06 #1
1 4779
<il***********@gmail.com> wrote:
So I see from the documentation here:
http://msdn.microsoft.com/library/de...library/en-us/
cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTo pic.asp

That the code uses the less than operator:
int myIndex=myList.BinarySearch( myObject );
if ( myIndex < 0 )

when using the BinarySearch method and in my own experience I see that
sometimes it returns other negative values (other than -1).

What is the significance of the various negative values? I'm used to
methods that return indices to return only -1 on failure - do these
other negative values tell you how many times it had to divide and
conquer to reach the conclusion that the value doesn't occur in the
ArrayList?


From the first overload in the documentation you linked to:

<quote>
Return Value

The zero-based index of value in the sorted ArrayList, if value is
found; otherwise, a negative number, which is the bitwise complement of
the index of the next element that is larger than value or, if there is
no larger element, the bitwise complement of Count.
</quote>

and

<quote>
If the ArrayList does not contain the specified value, the method
returns a negative integer. You can apply the bitwise complement
operation (~) to this negative integer to get the index of the first
element that is larger than the search value. When inserting the value
into the ArrayList, this index should be used as the insertion point to
maintain the sort order.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eric Eggermann | last post by:
I'm going batty trying to figure out why this is not working. I'm trying to find all objects within a class derived from CollectionBase which meet a certain criteria, but the search seems to be...
8
by: Sek | last post by:
Folks, I have an ArrayList of integers. I have sorted the list already. Now, i want to find the index of the first element that is greater than a given number. How to accomplish this in C#?
4
by: Bernie Yaeger | last post by:
I'm having difficulty searching an arraylist for certain values that are in another arraylist. Essentially, I want to load the second arraylist with only one of each of the possible elements in...
4
by: Mike Dole | last post by:
I have an arraylist like the one with the Guitar Class sample in Q316302. Dim MycolliCol as arraylist Private Sub FillArray() MyColliCol.Add(New Colli(1, "STUK", 0)) MyColliCol.Add(New...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
2
by: Henry Padilla | last post by:
I have a list of strings and I would like to insert them in order as they come up. I am trying to use ArrayList.BinarySearch which (theoretically) returns the negative bitwise compliment. And I...
10
by: JohnR | last post by:
I have an arraylist of string values. I would like to search the arraylist to find the index of a particular string and I would like the search to be case insensitive. dim al as new arraylist...
3
by: Justin | last post by:
Here's a quick rundown of what I'm doing. I'm filling an arraylist with data. Then I loop through a dataset and grab a field to perform a search on the arraylist. Every time I find a match I...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.