Yasser
Yasser

Reputation: 325

get Item Index of arrayList knowing its Name

is there a function that take array list item and bring back item index ?

for ex:

int indexItem = ArrayList.getItemIndexByName(itemName);

Upvotes: 0

Views: 20890

Answers (3)

eandersson
eandersson

Reputation: 26362

I think that you are looking for IndexOf.

int indexItem = myArray.IndexOf(itemName);

You can use it like this.

String itemName = "Hello";
ArrayList myArray = new ArrayList();
myArray.Add( "Hello" );
myArray.Add( "World" );

// Get index of "Hello"
int indexItem = myArray.IndexOf(itemName);

Upvotes: 2

ColinE
ColinE

Reputation: 70170

There is no single method that will give you this, but you could first search for your item by name, then use ArrayList.IndexOf.

Upvotes: 1

Nathan
Nathan

Reputation: 6216

Tried storing your items in a Dictionary<string, MyItemType> ?

Upvotes: 0

Related Questions