PC Review


Reply
Thread Tools Rate Thread

How do I best find the index for an object in a generic list

 
 
Tony Johansson
Guest
Posts: n/a
 
      17th Mar 2011
Hello!

I have a collection of List<Player> and I want to find the index where the
passed name match the player name in the collection.
Is the only solution to this to loop through the collection and check each
object.

I assume there is a better way

//Tony


 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      17th Mar 2011
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:ilto87$64k$(E-Mail Removed)...

> I have a collection of List<Player> and I want to find the index where the
> passed name match the player name in the collection.
> Is the only solution to this to loop through the collection and check each
> object.
>
> I assume there is a better way


Then you make the same incorrect assumption that a lot of people do, because
ultimately a loop will be involved. Now, if you mean there must be a better
way than explicitly writing loop code, you could take advantage of extension
methods like Find(), assuming you're using .NET 3.x or higher. But there
WILL be a loop....


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      17th Mar 2011
On 17-03-2011 15:43, Tony Johansson wrote:
> I have a collection of List<Player> and I want to find the index where the
> passed name match the player name in the collection.
> Is the only solution to this to loop through the collection and check each
> object.
>
> I assume there is a better way


The operation will always be a O(n) operation, where at runtime
a loop will be executed.

You can avoid a loop in the source code by using something
builtin.

The List IndexOf methods seems an obvious choice.

For that to work properly you should override Equals
and GetHashCode in the Player class.

But that is probably a good idea anyway.

Arne

 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      18th Mar 2011

"Arne Vajhøj" <(E-Mail Removed)> skrev i meddelandet
news:4d82746a$0$23763$(E-Mail Removed)...
> On 17-03-2011 15:43, Tony Johansson wrote:
>> I have a collection of List<Player> and I want to find the index where
>> the
>> passed name match the player name in the collection.
>> Is the only solution to this to loop through the collection and check
>> each
>> object.
>>
>> I assume there is a better way

>
> The operation will always be a O(n) operation, where at runtime
> a loop will be executed.
>
> You can avoid a loop in the source code by using something
> builtin.
>
> The List IndexOf methods seems an obvious choice.
>
> For that to work properly you should override Equals
> and GetHashCode in the Player class.
>
> But that is probably a good idea anyway.
>
> Arne
>


I have a generic collection List<Players> with several Players in the list.
Each player has several fields but one field is called name.
Now I want to find the index to the player in the list with name "putte" for
example
I now I can use a simple loop but I want to use some extension feature that
hide the looping.
So how to a write that ?

//Tony


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      18th Mar 2011
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:ilva64$i42$(E-Mail Removed)...

> I have a generic collection List<Players> with several Players in the
> list.
> Each player has several fields but one field is called name.
> Now I want to find the index to the player in the list with name "putte"
> for example
> I now I can use a simple loop but I want to use some extension feature
> that hide the looping.
> So how to a write that ?


I'm sure there's a lambda-expression way, but I'm no expert so here's the
delegate way:

string nameToFind = "Tony";
int index = myPlayerList.FindIndex(delegate(Player p)
{
return p.Name == nameToFind; // Or use some other comparison if you want
case-insensitivity
});

I hope I did that right; I don't use extension methods and anonymous
delegates very often.


 
Reply With Quote
 
Dude
Guest
Posts: n/a
 
      18th Mar 2011
On Mar 17, 3:43*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> Hello!
>
> I have a collection of List<Player> and I want to find the index where the
> passed name match the player name in the collection.
> Is the only solution to this to loop through the collection and check each
> object.
>
> I assume there is a better way
>
> //Tony



If the better way is to use something more efficient. Instead of
using a List<> which implies a list that will most likely always be
read end-end. Instead use a Dictionary<Key,Value> which expects a
more key based lookup.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Array to Generic List and find object in list Chris Kennedy Microsoft Dot NET Framework 1 31st Aug 2008 08:04 PM
Using ForEach in a Generic List object. Doug Microsoft Dot NET Framework 9 10th Aug 2007 05:59 PM
Using ForEach method of Generic List Object. Doug Microsoft C# .NET 1 6th Aug 2007 05:54 PM
Generic List object Sehboo Microsoft C# .NET 7 15th Feb 2007 08:52 PM
find out whether there already is an object of the same derived typein a generic collection Fabian Microsoft Dot NET Framework 1 23rd Jun 2006 03:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:09 AM.