473,657 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList.Binar ySearch 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 4803
<il***********@ gmail.com> wrote:
So I see from the documentation here:
http://msdn.microsoft.com/library/de...library/en-us/
cpref/html/frlrfSystemColl ectionsArrayLis tClassBinarySea rchTopic.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.co m>
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
3903
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 skipping some objects. The following code is supposed to start searching the InnerList from the beginning, and continue searching while there are still objects that my comparer class says matches. In the code, comp is my comparer object, and...
8
11017
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
1304
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 the first arraylist. Thus if singlearraylist contains "ww", "l2", "s1", "ww", "cc", "s1" I only want marraylist to have "ww", "l2", "s1", "cc". The search seems to fail regardless what I do, and the result is that marraylist has virtually all of...
4
1521
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 Colli(16, "ROL", 1)) MyColliCol.Add(New Colli(17, "BOS", 2)) MyColliCol.Add(New Colli(18, "DOOS", 0)) MyColliCol.Add(New Colli(19, "PAK", 0))
18
4728
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
2
1503
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 should be able to find the index of the element one higher than where I should insert. How? I'm not getting it.
10
9678
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 al.add("one") al.add("two") dim x as integer = al.indexof("ONE") I would like this to find the match and return 0
3
3094
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 update another field with a 1. If I don't find a match I update it with a 0. I then remove that item from the arraylist and move on. The end result of this is I know which items ARE in the dataset, which items ARE NOT in the dataset and which...
8
2587
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; m_ResultArray.Add ( aFoundObject);
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.