How to use ArrayList.BinarySearch if the object is not found.

H

Henry Padilla

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.

I created an arraylist with strings "1", "3", "5" and when I did a
BinarySearch for "2" I get (-100001). The bitwise compliment of which is
(100000), not (1) like I should expect.

And a BinarySearch for "4" returns (-200002).

I've got no idea what this is doing. How do you use this function?

Thanks in advance,
Tom Padilla
 
C

Chris Dunaway

When I run the following code:

Dim al As New ArrayList

al.Add("1")
al.Add("3")
al.Add("5")

MsgBox((Not al.BinarySearch("4")).ToString)


I get 2 in the MsgBox. Are the items in your ArrayList sorted? They
must be sorted for the BinarySearch method to work.
 
H

Henry Padilla

Chris Dunaway said:
When I run the following code:

Dim al As New ArrayList

al.Add("1")
al.Add("3")
al.Add("5")

MsgBox((Not al.BinarySearch("4")).ToString)


I get 2 in the MsgBox. Are the items in your ArrayList sorted? They
must be sorted for the BinarySearch method to work.

That was it.

Just "NOT".

Thanks man. I was using XOR, XOR -1, -1 + XOR, NOR, NOR x -1... all kinds
of crap.

Thanks,
Tom P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top