473,327 Members | 1,920 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,327 software developers and data experts.

ArrayList.BinarySearch

Does anyone know why this snippet of code continues to return x with a value
of -1?
Is there an issue with ArrayList.BinarySearch?

ArrayList myAL = new ArrayList();

myAL .Add(1);

myAL .Add(2);

myAL .Add(3);

object oX;

oX = 3;

int x = myAL .BinarySearch(oX)

According to the documentation, x should = 2. Being the index of where the
value 3 exists in myAl.

Regards

Pete


Nov 15 '05 #1
4 6779
Pete:

Stupid question but is this the actual example....ie is the list definitely
sorted? If not, binary search does give some weird results...
"Pete Z" <slim> wrote in message
news:Of*************@TK2MSFTNGP10.phx.gbl...
Does anyone know why this snippet of code continues to return x with a value of -1?
Is there an issue with ArrayList.BinarySearch?

ArrayList myAL = new ArrayList();

myAL .Add(1);

myAL .Add(2);

myAL .Add(3);

object oX;

oX = 3;

int x = myAL .BinarySearch(oX)

According to the documentation, x should = 2. Being the index of where the value 3 exists in myAl.

Regards

Pete

Nov 15 '05 #2
Hi William,
Does the ArrayList need to be sorted for BinarySearch to work?
Is that the message I'm getting?

Regards
Pete

"William Ryan" <do********@nospam.comcast.net> wrote in message
news:Og**************@TK2MSFTNGP11.phx.gbl...
Pete:

Stupid question but is this the actual example....ie is the list definitely sorted? If not, binary search does give some weird results...
"Pete Z" <slim> wrote in message
news:Of*************@TK2MSFTNGP10.phx.gbl...
Does anyone know why this snippet of code continues to return x with a

value
of -1?
Is there an issue with ArrayList.BinarySearch?

ArrayList myAL = new ArrayList();

myAL .Add(1);

myAL .Add(2);

myAL .Add(3);

object oX;

oX = 3;

int x = myAL .BinarySearch(oX)

According to the documentation, x should = 2. Being the index of where

the
value 3 exists in myAl.

Regards

Pete


Nov 15 '05 #3
<"Pete Z" <slim>> wrote:
Does the ArrayList need to be sorted for BinarySearch to work?
Is that the message I'm getting?


It's the message from the documentation :)

<quote>
Uses a binary search algorithm to locate a specific element in the
sorted ArrayList or a portion of it.
</quote>

If the list weren't sorted to start with, there's no way a binary
search would work, if you think about how a binary search works.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Thanks guys!
Yes, it quite clearly states it on the first line of the documentation.
Regards
Pete
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Pete Z" <slim>> wrote:
Does the ArrayList need to be sorted for BinarySearch to work?
Is that the message I'm getting?


It's the message from the documentation :)

<quote>
Uses a binary search algorithm to locate a specific element in the
sorted ArrayList or a portion of it.
</quote>

If the list weren't sorted to start with, there's no way a binary
search would work, if you think about how a binary search works.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5

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

Similar topics

4
by: Homa | last post by:
I can' believe my own eye, but it happens...there is a bug in ArrayList.BinarySearch!! It should be such a simple function...... Here is the detail. (I'm using C#, don't know if this is C#'s...
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: 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...
9
by: Paul Nations | last post by:
I've got arraylists of simple classes bound to controls. I need to search through those arraylists to set the correct SelectedItem in the control. The code looks like: Public Class...
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...
6
by: John Veldthuis | last post by:
I have an ArrayList set up with a set of class objects. The class has 3 strings in it and are as follows ProdID GSP Description Okay now problem with getting all these into the list and...
1
by: garyusenet | last post by:
My Array list contains a collection of InternetExplorer object. One of properties of this object is HWND. I'm trying to search my arraylist for the InternetExplorer object that has a certain...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.