473,608 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("0 04876497")
SAPArray.Add("4 8764205")
SAPArray.Add("4 8764212")
SAPArray.Add("4 8764229")
SAPArray.Add("4 8764236")
SAPArray.Add("4 8764243")
SAPArray.Add("4 8764250")
SAPArray.Add("4 8764267")
SAPArray.Add("4 8764274")
SAPArray.Add("4 8764281")
SAPArray.Add("4 8764298")
SAPArray.Add("4 8764304")
SAPArray.Add("4 8764311")
SAPArray.Add("4 8764328")
SAPArray.Add("4 8764335")
SAPArray.Add("4 8764342")
SAPArray.Add("4 8764359")
SAPArray.Add("4 8764366")
SAPArray.Add("4 8764373")
SAPArray.Add("4 8764380")
SAPArray.Add("4 8764397")
SAPArray.Add("1 1111111")
SAPArray.Add("2 22222222")
SAPArray.Add("3 333333333")
SAPArray.Add("4 4444444444")
SAPArray.Add("5 55555555555")
SAPArray.Add("6 666666666666")
SAPArray.Add("7 7777777777777")
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.Binary Search(GetTagIn fo(tags(i).TagI D)(5),
New CaseInsensitive Comparer())
Any insight would be greatly appreciated!
Dec 5 '06 #1
3 3092
We can even shorten up the issue. Let's say this is my array:

SAPArray.Add("0 4876497")
SAPArray.Add("4 8764205")
SAPArray.Add("4 8764212")
SAPArray.Add("4 8764229")
SAPArray.Add("4 8764236")
SAPArray.Add("4 8764243")
SAPArray.Add("4 8764250")
SAPArray.Add("4 8764267")
SAPArray.Add("4 8764274")
SAPArray.Add("4 8764281")
SAPArray.Add("4 8764298")
SAPArray.Add("4 8764304")
SAPArray.Add("4 8764311")
SAPArray.Add("4 8764328")
SAPArray.Add("4 8764335")
SAPArray.Add("4 8764342")
SAPArray.Add("4 8764359")
SAPArray.Add("4 8764366")
SAPArray.Add("4 8764373")
SAPArray.Add("4 8764380")
SAPArray.Add("4 8764397")
SAPArray.Add("1 1111111")
SAPArray.Add("2 2222222")
SAPArray.Add("3 3333333")
SAPArray.Add("4 4444444")
SAPArray.Add("5 5555555")
SAPArray.Add("6 6666666")
SAPArray.Add("7 7777777")

When I run this code directly after the array creation:
Dim index2 As Integer = SAPArray.Binary Search("4876437 3", New
CaseInsensitive Comparer())
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("4 8764205")
SAPArray.Add("4 8764212")
SAPArray.Add("4 8764229")
SAPArray.Add("4 8764236")
SAPArray.Add("4 8764243")
SAPArray.Add("4 8764250")
SAPArray.Add("4 8764267")
SAPArray.Add("4 8764274")
SAPArray.Add("4 8764281")
SAPArray.Add("4 8764298")
SAPArray.Add("4 8764304")
SAPArray.Add("4 8764311")
SAPArray.Add("4 8764328")
SAPArray.Add("4 8764335")
SAPArray.Add("4 8764342")
SAPArray.Add("4 8764359")
SAPArray.Add("4 8764366")
SAPArray.Add("4 8764373")
SAPArray.Add("4 8764380")
SAPArray.Add("4 8764397")
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("0 4876497")
SAPArray.Add("4 8764205")
SAPArray.Add("4 8764212")
SAPArray.Add("4 8764229")
SAPArray.Add("4 8764236")
SAPArray.Add("4 8764243")
SAPArray.Add("4 8764250")
SAPArray.Add("4 8764267")
SAPArray.Add("4 8764274")
SAPArray.Add("4 8764281")
SAPArray.Add("4 8764298")
SAPArray.Add("4 8764304")
SAPArray.Add("4 8764311")
SAPArray.Add("4 8764328")
SAPArray.Add("4 8764335")
SAPArray.Add("4 8764342")
SAPArray.Add("4 8764359")
SAPArray.Add("4 8764366")
SAPArray.Add("4 8764373")
SAPArray.Add("4 8764380")
SAPArray.Add("4 8764397")
SAPArray.Add("1 1111111")
SAPArray.Add("2 2222222")
SAPArray.Add("3 3333333")
SAPArray.Add("4 4444444")
SAPArray.Add("5 5555555")
SAPArray.Add("6 6666666")
SAPArray.Add("7 7777777")

When I run this code directly after the array creation:
Dim index2 As Integer = SAPArray.Binary Search("4876437 3", New
CaseInsensitive Comparer())
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.c om
www.maisto.com
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atwrote in message
news:Oe******** ******@TK2MSFTN GP03.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.Binary Search("4876437 3", New
CaseInsensitiv eComparer())
MsgBox(index 2)

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
4521
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 problem or the whole framework's problem. ArrayList test = new ArrayList(); test.Add(10); test.Add(7);
4
6795
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 .Add(2);
8
11016
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
1303
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
1519
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))
9
2867
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 DegreeMaintenance Private arrCipCodes As New ArrayList 'populate reader with data With rdr Do While .Read arrCipCodes.Add(New CipCode(.GetString(0), .GetString(1)))
10
9665
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
6
4065
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 displaying them correctly and manipulating things except for one thing.
8
2585
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
7998
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
8470
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
8142
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
8329
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...
0
6813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6010
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
3959
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...
1
1580
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1327
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.