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

Confusing Arraylist BinarySearch problem

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 items do not BELONG in the dataset.

The problem I'm running into is when the arraylist contains items that are
not in the dataset. For some reason, during that scenario I get odd index
results when I search the arraylist.

Here's the arraylist (hardcoded for testing):
SAPArray.Add("004876497")
SAPArray.Add("48764205")
SAPArray.Add("48764212")
SAPArray.Add("48764229")
SAPArray.Add("48764236")
SAPArray.Add("48764243")
SAPArray.Add("48764250")
SAPArray.Add("48764267")
SAPArray.Add("48764274")
SAPArray.Add("48764281")
SAPArray.Add("48764298")
SAPArray.Add("48764304")
SAPArray.Add("48764311")
SAPArray.Add("48764328")
SAPArray.Add("48764335")
SAPArray.Add("48764342")
SAPArray.Add("48764359")
SAPArray.Add("48764366")
SAPArray.Add("48764373")
SAPArray.Add("48764380")
SAPArray.Add("48764397")
SAPArray.Add("11111111")
SAPArray.Add("222222222")
SAPArray.Add("3333333333")
SAPArray.Add("44444444444")
SAPArray.Add("555555555555")
SAPArray.Add("6666666666666")
SAPArray.Add("77777777777777")
The last 1-2-3-4-5-6-7 fields are purposely bogus.

Here's the resulting list I've created of the found index values when search
the arraylist for each one of these entries

004876497 0
48764205 0
48764212 0
48764229 0
48764243 1
48764236 0
48764366 -20
48764397 -20
48764380 -20
48764342 9
48764373 -19
48764304 5
48764359 8
48764335 7
48764250 0
48764274 1
48764328 4
48764311 3
48764281 1
48764298 -11
48764267 -11

A couple notes:
1. The last 1-2-3-4-5-6-7 fields don't show up because I only search based
on what's in the dataset and these do not apply.
2. It's possible to have duplicate index values because I'm removing the
found items as well.

What I don't understand, for example is that 48764366 has an index value
of -20 even though it is in fact in the arraylist. I also don't understand
what the difference is between the different negative values. Shouldn't a
null result just be -1?

My search string:
Dim index As Integer = SAPArray.BinarySearch(GetTagInfo(tags(i).TagID)(5) ,
New CaseInsensitiveComparer())
Any insight would be greatly appreciated!
Dec 5 '06 #1
3 3077
We can even shorten up the issue. Let's say this is my array:

SAPArray.Add("04876497")
SAPArray.Add("48764205")
SAPArray.Add("48764212")
SAPArray.Add("48764229")
SAPArray.Add("48764236")
SAPArray.Add("48764243")
SAPArray.Add("48764250")
SAPArray.Add("48764267")
SAPArray.Add("48764274")
SAPArray.Add("48764281")
SAPArray.Add("48764298")
SAPArray.Add("48764304")
SAPArray.Add("48764311")
SAPArray.Add("48764328")
SAPArray.Add("48764335")
SAPArray.Add("48764342")
SAPArray.Add("48764359")
SAPArray.Add("48764366")
SAPArray.Add("48764373")
SAPArray.Add("48764380")
SAPArray.Add("48764397")
SAPArray.Add("11111111")
SAPArray.Add("22222222")
SAPArray.Add("33333333")
SAPArray.Add("44444444")
SAPArray.Add("55555555")
SAPArray.Add("66666666")
SAPArray.Add("77777777")

When I run this code directly after the array creation:
Dim index2 As Integer = SAPArray.BinarySearch("48764373", New
CaseInsensitiveComparer())
MsgBox(index2)

I get a negative value even though that item exists. For some reason
certain index values are more problematic them others. If I search for one
that's usual comes up good I get the proper index value.

But when I shorten my array to this:

SAPArray.Add("48764205")
SAPArray.Add("48764212")
SAPArray.Add("48764229")
SAPArray.Add("48764236")
SAPArray.Add("48764243")
SAPArray.Add("48764250")
SAPArray.Add("48764267")
SAPArray.Add("48764274")
SAPArray.Add("48764281")
SAPArray.Add("48764298")
SAPArray.Add("48764304")
SAPArray.Add("48764311")
SAPArray.Add("48764328")
SAPArray.Add("48764335")
SAPArray.Add("48764342")
SAPArray.Add("48764359")
SAPArray.Add("48764366")
SAPArray.Add("48764373")
SAPArray.Add("48764380")
SAPArray.Add("48764397")
I have no problems. The above code returns the proper index value.
Dec 5 '06 #2
"Justin" <Ju****@NoSpam.comschrieb:
We can even shorten up the issue. Let's say this is my array:

SAPArray.Add("04876497")
SAPArray.Add("48764205")
SAPArray.Add("48764212")
SAPArray.Add("48764229")
SAPArray.Add("48764236")
SAPArray.Add("48764243")
SAPArray.Add("48764250")
SAPArray.Add("48764267")
SAPArray.Add("48764274")
SAPArray.Add("48764281")
SAPArray.Add("48764298")
SAPArray.Add("48764304")
SAPArray.Add("48764311")
SAPArray.Add("48764328")
SAPArray.Add("48764335")
SAPArray.Add("48764342")
SAPArray.Add("48764359")
SAPArray.Add("48764366")
SAPArray.Add("48764373")
SAPArray.Add("48764380")
SAPArray.Add("48764397")
SAPArray.Add("11111111")
SAPArray.Add("22222222")
SAPArray.Add("33333333")
SAPArray.Add("44444444")
SAPArray.Add("55555555")
SAPArray.Add("66666666")
SAPArray.Add("77777777")

When I run this code directly after the array creation:
Dim index2 As Integer = SAPArray.BinarySearch("48764373", New
CaseInsensitiveComparer())
MsgBox(index2)

I get a negative value even though that item exists.
Items must be sorted in order to search an item using binary search. You
can call the arraylist's 'Sort' method to sort it.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Dec 5 '06 #3
SAPArray.Sort()

Perfect! Thanks a bunch! I never knew that. I guess I've just been lucky
this whole time or something else was always sorting my data prior to
creation.

Am I to assume that once it hit where it thinks the value should be it stops
looking? If so then that's probably a huge speed increase in large array
scenarios.

Thanks again!
Thanks, Justin Emlay SAP Basis Administrator / Systems Department Maisto
International, Inc. 909-357-7988 ext.360 909-357-9958 fax JE****@Maisto.com
www.maisto.com
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:Oe**************@TK2MSFTNGP03.phx.gbl...
"Justin" <Ju****@NoSpam.comschrieb:
>We can even shorten up the issue. Let's say this is my array:

SAPArray.Add("04876497")
SAPArray.Add("48764205")
SAPArray.Add("48764212")
SAPArray.Add("48764229")
SAPArray.Add("48764236")
SAPArray.Add("48764243")
SAPArray.Add("48764250")
SAPArray.Add("48764267")
SAPArray.Add("48764274")
SAPArray.Add("48764281")
SAPArray.Add("48764298")
SAPArray.Add("48764304")
SAPArray.Add("48764311")
SAPArray.Add("48764328")
SAPArray.Add("48764335")
SAPArray.Add("48764342")
SAPArray.Add("48764359")
SAPArray.Add("48764366")
SAPArray.Add("48764373")
SAPArray.Add("48764380")
SAPArray.Add("48764397")
SAPArray.Add("11111111")
SAPArray.Add("22222222")
SAPArray.Add("33333333")
SAPArray.Add("44444444")
SAPArray.Add("55555555")
SAPArray.Add("66666666")
SAPArray.Add("77777777")

When I run this code directly after the array creation:
Dim index2 As Integer = SAPArray.BinarySearch("48764373", New
CaseInsensitiveComparer())
MsgBox(index2)

I get a negative value even though that item exists.

Items must be sorted in order to search an item using binary search. You
can call the arraylist's 'Sort' method to sort it.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Dec 5 '06 #4

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...
4
by: Pete Z | last post by:
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...
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...
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...
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...
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
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.