473,396 Members | 2,090 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,396 software developers and data experts.

ArrayList Search for Value

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.

I wish to be able to do search through the whole list without stepping through the entire list
enumerating them. I would just like to be able to search on one field. The ProdID.

For now I do it by

dim myspec as new clsSpecs
for each myspec in specList
if test.prodid.compareto(myspec.ProdID) = 0 then

do stuff

end if

next
Now this works but I am looking for a search method but list.BinarySearch does not seem to do it.

Any help?
Nov 21 '05 #1
6 4048

"John Veldthuis" <jo***@no.spamo.paradise.net.nz> wrote in message
news:ma********************************@4ax.com...
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.

I wish to be able to do search through the whole list without stepping
through the entire list
enumerating them. I would just like to be able to search on one field. The
ProdID.

For now I do it by

dim myspec as new clsSpecs
for each myspec in specList
if test.prodid.compareto(myspec.ProdID) = 0 then

do stuff

end if

next
Now this works but I am looking for a search method but list.BinarySearch
does not seem to do it.

Any help?


I suggest you read this article:

http://msdn.microsoft.com/library/de...archtopic1.asp

You'll note that the objects in your ArrayList must come from a class that
implements the IComparable interface, and that the objects must be sorted
according to your IComparable implementation. Then the BinarySearch method
will yield the results you seek.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Nov 21 '05 #2
On Fri, 14 Oct 2005 22:57:42 -0500, "Peter van der Goes" <p_**********@toadstool.u> wrote:

Thanks. I had implemented IComparable to sort the list but was using it on the GSP entry.
I changed it to look at the ProdID and it did return what I expected. Changed it back as that was
what I needed the main sort on.

However I added an IComparable called FindProdID and used the following

myspec = new clsSpecs
mySpec.ProdID = "100676"
indx = specList.BinarySearch(myspec, new FindProdID)

and all worked well and returned the index it should have.

Thanks for the pointer.

"John Veldthuis" <jo***@no.spamo.paradise.net.nz> wrote in message
news:ma********************************@4ax.com.. .
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.

I wish to be able to do search through the whole list without stepping
through the entire list
enumerating them. I would just like to be able to search on one field. The
ProdID.

For now I do it by

dim myspec as new clsSpecs
for each myspec in specList
if test.prodid.compareto(myspec.ProdID) = 0 then

do stuff

end if

next
Now this works but I am looking for a search method but list.BinarySearch
does not seem to do it.

Any help?


I suggest you read this article:

http://msdn.microsoft.com/library/de...archtopic1.asp

You'll note that the objects in your ArrayList must come from a class that
implements the IComparable interface, and that the objects must be sorted
according to your IComparable implementation. Then the BinarySearch method
will yield the results you seek.

Nov 21 '05 #3
John,

If you are sure that you want to use the complete word in the exact cases,
than you can use the arraylist indexof method

http://msdn.microsoft.com/library/de...dexoftopic.asp

If you want it undependable from the cases than you should be able to use
the Icomparer as nicely described by Peter Proost, Jay B. Harlow and more
people in this newsgroup this week

http://groups.google.com/group/micro...9adf0d52e87dfc

If you are the same John, than forget this message.

I hope this helps,

Cor
Nov 21 '05 #4
Don't you have to sort the arraylist on the ProdID property first before
using the binarysearch?
--
Dennis in Houston
"John Veldthuis" wrote:
On Fri, 14 Oct 2005 22:57:42 -0500, "Peter van der Goes" <p_**********@toadstool.u> wrote:

Thanks. I had implemented IComparable to sort the list but was using it on the GSP entry.
I changed it to look at the ProdID and it did return what I expected. Changed it back as that was
what I needed the main sort on.

However I added an IComparable called FindProdID and used the following

myspec = new clsSpecs
mySpec.ProdID = "100676"
indx = specList.BinarySearch(myspec, new FindProdID)

and all worked well and returned the index it should have.

Thanks for the pointer.

"John Veldthuis" <jo***@no.spamo.paradise.net.nz> wrote in message
news:ma********************************@4ax.com.. .
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.

I wish to be able to do search through the whole list without stepping
through the entire list
enumerating them. I would just like to be able to search on one field. The
ProdID.

For now I do it by

dim myspec as new clsSpecs
for each myspec in specList
if test.prodid.compareto(myspec.ProdID) = 0 then

do stuff

end if

next
Now this works but I am looking for a search method but list.BinarySearch
does not seem to do it.

Any help?


I suggest you read this article:

http://msdn.microsoft.com/library/de...archtopic1.asp

You'll note that the objects in your ArrayList must come from a class that
implements the IComparable interface, and that the objects must be sorted
according to your IComparable implementation. Then the BinarySearch method
will yield the results you seek.

Nov 21 '05 #5
On Sat, 15 Oct 2005 06:01:02 -0700, Dennis <De****@discussions.microsoft.com> wrote:
Don't you have to sort the arraylist on the ProdID property first before
using the binarysearch?


Yes, I simply resorted using the new IComparer and then did the search and then resorted back the
original.
Nov 21 '05 #6
On Sat, 15 Oct 2005 11:08:58 +0200, "Cor Ligthert [MVP]" <no************@planet.nl> wrote:
John,

If you are sure that you want to use the complete word in the exact cases,
than you can use the arraylist indexof method

http://msdn.microsoft.com/library/de...dexoftopic.asp

If you want it undependable from the cases than you should be able to use
the Icomparer as nicely described by Peter Proost, Jay B. Harlow and more
people in this newsgroup this week

http://groups.google.com/group/micro...9adf0d52e87dfc

If you are the same John, than forget this message.

I hope this helps,


Thanks, different John but I understood all that. My problem was that I did not just have a simple
entry as the example but a class and was unsure of how the binarysearch worked on only filling out
part of the class. However all fixed now and working 100% how I expected thanks to the help and
pointers from those here.
Nov 21 '05 #7

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

Similar topics

6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
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 | last post by:
I have declared two classes. The first class has 4 private variables. Each has a property defined. I'm calling a readfile sub from a second class. The second class also has an Arraylist...
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...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
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...
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...
3
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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,...
0
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...
0
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...
0
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,...

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.