471,337 Members | 829 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 1731
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Randy | last post: by
4 posts views Thread by Tamir Khason | last post: by
2 posts views Thread by dotNetDave | last post: by
3 posts views Thread by Fred | last post: by
10 posts views Thread by C Downey | last post: by
9 posts views Thread by Paul Nations | last post: by
4 posts views Thread by MikeY | last post: by
4 posts views Thread by Jon Paal | last post: by

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.