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

T

Tony Johansson

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
 
J

Jeff Johnson

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....
 
A

Arne Vajhøj

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
 
T

Tony Johansson

Arne Vajhøj said:
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
 
J

Jeff Johnson

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.
 
D

Dude

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top