473,597 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Searching for a particular object in an arraylist

AAJ
Hi all

I have some objects that I add to an ArrayList. Each object is from the same
class and has methods etc. Each can be identified by a unique property, in
this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array List
using the .PK property. I know I could use a FOREACH loop, and check each PK
of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.Binar ySearch, but this seems to compare the full object, but its
the PK that I need to find, and then return the index of the located object.

I can find references to Icomparable, Icompare, I can find references to
finding integers, strings etc, but I cant find searching for a particular
property. I am also aware that for a binary search, the list needs to be
sorted.

can any one point me to a tutorial, perhaps I should be using a hashtable
and storing PK

andy advice would be welcome

thanks in advance

Andy
Feb 9 '06 #1
6 1822
Hi AAJ,
for fast retrieval of information a Hashtable (or Dictionary in 2.0) is
definitely the way to go, since you have Random Access capabilities rather
than employing some search algorithmn.

If you are goign to use your objects in the hashtable as a key then make
sure you have overriden the Equals and GetHashCode method.

Mark Dawson
http://www.markdawson.org


"AAJ" wrote:
Hi all

I have some objects that I add to an ArrayList. Each object is from the same
class and has methods etc. Each can be identified by a unique property, in
this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array List
using the .PK property. I know I could use a FOREACH loop, and check each PK
of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.Binar ySearch, but this seems to compare the full object, but its
the PK that I need to find, and then return the index of the located object.

I can find references to Icomparable, Icompare, I can find references to
finding integers, strings etc, but I cant find searching for a particular
property. I am also aware that for a binary search, the list needs to be
sorted.

can any one point me to a tutorial, perhaps I should be using a hashtable
and storing PK

andy advice would be welcome

thanks in advance

Andy

Feb 9 '06 #2
AAJ
Cheers Mark

Hashtables it is!

many thanks

Andy

"Mark R. Dawson" <Ma*********@di scussions.micro soft.com> wrote in message
news:50******** *************** ***********@mic rosoft.com...
Hi AAJ,
for fast retrieval of information a Hashtable (or Dictionary in 2.0) is
definitely the way to go, since you have Random Access capabilities rather
than employing some search algorithmn.

If you are goign to use your objects in the hashtable as a key then make
sure you have overriden the Equals and GetHashCode method.

Mark Dawson
http://www.markdawson.org


"AAJ" wrote:
Hi all

I have some objects that I add to an ArrayList. Each object is from the
same
class and has methods etc. Each can be identified by a unique property,
in
this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array
List
using the .PK property. I know I could use a FOREACH loop, and check each
PK
of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.Binar ySearch, but this seems to compare the full object, but
its
the PK that I need to find, and then return the index of the located
object.

I can find references to Icomparable, Icompare, I can find references to
finding integers, strings etc, but I cant find searching for a
particular
property. I am also aware that for a binary search, the list needs to be
sorted.

can any one point me to a tutorial, perhaps I should be using a hashtable
and storing PK

andy advice would be welcome

thanks in advance

Andy

Feb 9 '06 #3
Andy,

1. Make your object implement IComparable.
2. In the CompareTo method needed for IComparable, compare the PK properties
3. Sort your ArrayList using the ArrayList.Sort method
4. Use the BinarySearch method to find an object.

Dave
"AAJ" <a.a.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Hi all

I have some objects that I add to an ArrayList. Each object is from the
same class and has methods etc. Each can be identified by a unique
property, in this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array
List using the .PK property. I know I could use a FOREACH loop, and check
each PK of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.Binar ySearch, but this seems to compare the full object, but its
the PK that I need to find, and then return the index of the located
object.

I can find references to Icomparable, Icompare, I can find references to
finding integers, strings etc, but I cant find searching for a particular
property. I am also aware that for a binary search, the list needs to be
sorted.

can any one point me to a tutorial, perhaps I should be using a hashtable
and storing PK

andy advice would be welcome

thanks in advance

Andy

Feb 9 '06 #4
For this particular case, I agree that IComparable is the right way to
go.

However, it should be pointed out that IComparable should be implemented
for the natural sort of an object. In other words, it is a way for the
object to indicate the way it should be compared to others of the same type.

If the sort order is different, then an implementation of IComparer
should be provided to the Sort and BinarySearch methods which would evaluate
the comparisons separate from the objects.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"D. Yates" <fo****@hotmail .com> wrote in message
news:ua******** ******@TK2MSFTN GP09.phx.gbl...
Andy,

1. Make your object implement IComparable.
2. In the CompareTo method needed for IComparable, compare the PK
properties
3. Sort your ArrayList using the ArrayList.Sort method
4. Use the BinarySearch method to find an object.

Dave
"AAJ" <a.a.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Hi all

I have some objects that I add to an ArrayList. Each object is from the
same class and has methods etc. Each can be identified by a unique
property, in this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array
List using the .PK property. I know I could use a FOREACH loop, and check
each PK of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.Binar ySearch, but this seems to compare the full object, but
its the PK that I need to find, and then return the index of the located
object.

I can find references to Icomparable, Icompare, I can find references to
finding integers, strings etc, but I cant find searching for a
particular property. I am also aware that for a binary search, the list
needs to be sorted.

can any one point me to a tutorial, perhaps I should be using a hashtable
and storing PK

andy advice would be welcome

thanks in advance

Andy


Feb 9 '06 #5
AAJ
Thanks all

I have used the hashtable method, but for the experience, I think I'll have
a go with IComparable as well

Andy
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:ur******** ******@TK2MSFTN GP09.phx.gbl...
For this particular case, I agree that IComparable is the right way to
go.

However, it should be pointed out that IComparable should be
implemented for the natural sort of an object. In other words, it is a
way for the object to indicate the way it should be compared to others of
the same type.

If the sort order is different, then an implementation of IComparer
should be provided to the Sort and BinarySearch methods which would
evaluate the comparisons separate from the objects.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"D. Yates" <fo****@hotmail .com> wrote in message
news:ua******** ******@TK2MSFTN GP09.phx.gbl...
Andy,

1. Make your object implement IComparable.
2. In the CompareTo method needed for IComparable, compare the PK
properties
3. Sort your ArrayList using the ArrayList.Sort method
4. Use the BinarySearch method to find an object.

Dave
"AAJ" <a.a.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Hi all

I have some objects that I add to an ArrayList. Each object is from the
same class and has methods etc. Each can be identified by a unique
property, in this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array
List using the .PK property. I know I could use a FOREACH loop, and
check each PK of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.Binar ySearch, but this seems to compare the full object, but
its the PK that I need to find, and then return the index of the located
object.

I can find references to Icomparable, Icompare, I can find references to
finding integers, strings etc, but I cant find searching for a
particular property. I am also aware that for a binary search, the list
needs to be sorted.

can any one point me to a tutorial, perhaps I should be using a
hashtable and storing PK

andy advice would be welcome

thanks in advance

Andy



Feb 10 '06 #6
AAJ
Just a final note to say thanks

have hashtables, sorting and BinarySearching all working now

thanks All

"AAJ" <a.a.com> wrote in message
news:ue******** ******@TK2MSFTN GP11.phx.gbl...
Thanks all

I have used the hashtable method, but for the experience, I think I'll
have a go with IComparable as well

Andy
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:ur******** ******@TK2MSFTN GP09.phx.gbl...
For this particular case, I agree that IComparable is the right way to
go.

However, it should be pointed out that IComparable should be
implemented for the natural sort of an object. In other words, it is a
way for the object to indicate the way it should be compared to others of
the same type.

If the sort order is different, then an implementation of IComparer
should be provided to the Sort and BinarySearch methods which would
evaluate the comparisons separate from the objects.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"D. Yates" <fo****@hotmail .com> wrote in message
news:ua******** ******@TK2MSFTN GP09.phx.gbl...
Andy,

1. Make your object implement IComparable.
2. In the CompareTo method needed for IComparable, compare the PK
properties
3. Sort your ArrayList using the ArrayList.Sort method
4. Use the BinarySearch method to find an object.

Dave
"AAJ" <a.a.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Hi all

I have some objects that I add to an ArrayList. Each object is from the
same class and has methods etc. Each can be identified by a unique
property, in this case PK

e.g.

PK =1 for Instance1.PK
PK =2 for Instance2.PK

etc

I need to be able to locate the index of a specific PK within the Array
List using the .PK property. I know I could use a FOREACH loop, and
check each PK of the Current.PK until I find the correct one.

I'm hoping there is a built in way of doing this. I looked at
ArrayList.Binar ySearch, but this seems to compare the full object, but
its the PK that I need to find, and then return the index of the
located object.

I can find references to Icomparable, Icompare, I can find references
to finding integers, strings etc, but I cant find searching for a
particular property. I am also aware that for a binary search, the list
needs to be sorted.

can any one point me to a tutorial, perhaps I should be using a
hashtable and storing PK

andy advice would be welcome

thanks in advance

Andy



Feb 10 '06 #7

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

Similar topics

2
1426
by: Randy | last post by:
I'm looking for an easy way to find the value member of an object in a control. I have an ArrayList of a simple class with two properties. One property is "Id" and the other is "CompName" (both stings). On my form I have a CheckListBox and I bind the ArrayList of Company's like so. Clb.DisplayMember = "CompName" Clb.ValueMember = "Id"
4
7077
by: Tamir Khason | last post by:
How can I set the type of the object added to ArrayList (type of Array List Members) Here is the code: protected ArrayList tabs = new ArrayList(); public ArrayList Tabs {
2
1123
by: dotNetDave | last post by:
I have implemented custom sorting and searches on my complex objects (they contain properties and collections) using IComparer and BinarySearch. I knew they before doing a search you had to sort your collection, but I found out the hard way yesterday that it has to be sorted on the same property as you are searching on. Fine. But now my problem is that while displaying this collection data to the user I have to search on a different...
3
9686
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
10
1507
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an object with that same ID already exists in the arraylist. If it does, I would like to increase the count of the matching object inside
0
951
by: Emily | last post by:
I am new to VB.NET and I need help finding and updating values in an arraylist. My arraylist is called arrVarInfo and holds data in this structure type: Public Structure VarInfo Dim Name As String Dim Format As String Dim Desc As String
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)))
4
3608
by: MikeY | last post by:
Hi Everyone, I'm am trying to find the best method option for searching through an ArrayList and getting various items at a time. It could be 0 to 14, 15 to 25 etc,etc. I wish .GetRange(0,45) would give more options than just zero base to whatever. Right now, (unless there is a better option) I will keep calling the ArrayList at various times getting the 15 or less items at a time and put the items into another ArrayList for my usage....
4
1518
by: Jon Paal | last post by:
how can I search for a value in a multi - item arraylist to prevent adding duplicates ?
0
7969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7886
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
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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...
0
6688
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...
0
3886
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...
0
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.