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

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.BinarySearch, 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 1812
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.BinarySearch, 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*********@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.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.BinarySearch, 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***************@TK2MSFTNGP10.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.BinarySearch, 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.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:ua**************@TK2MSFTNGP09.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***************@TK2MSFTNGP10.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.BinarySearch, 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.com> wrote in
message news:ur**************@TK2MSFTNGP09.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.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:ua**************@TK2MSFTNGP09.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***************@TK2MSFTNGP10.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.BinarySearch, 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**************@TK2MSFTNGP11.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.com> wrote
in message news:ur**************@TK2MSFTNGP09.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.com

"D. Yates" <fo****@hotmail.com> wrote in message
news:ua**************@TK2MSFTNGP09.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***************@TK2MSFTNGP10.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.BinarySearch, 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
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...
4
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
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...
3
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...
10
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...
0
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...
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...
4
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)...
4
by: Jon Paal | last post by:
how can I search for a value in a multi - item arraylist to prevent adding duplicates ?
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: 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
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,...
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.