Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find index of an int in a list

Tags:

c#

find

list

Is there a way to get the index of an int from a list? Looking for something like list1.FindIndex(5) where I want to find the position of 5 in the list.

like image 287
soandos Avatar asked Jun 26 '11 02:06

soandos


People also ask

How do you find the index of an int in a list?

Use the . IndexOf() method of the list. Specs for the method can be found on MSDN. Show activity on this post.

How do you find the index value of an element in a list?

The list index() method helps you to find the index of the given element. This is the easiest and straightforward way to get the index. The list index() method returns the index of the given element.

How do you find the index of an element in a number list in Python?

The index() method returns the index of the given element in the list. If the element is not found, a ValueError exception is raised.

How do you find the index of an element in a list in Java?

The indexOf(Object) method of the java. util. ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Using this method, you can find the index of a given element.


Video Answer


2 Answers

Use the .IndexOf() method of the list. Specs for the method can be found on MSDN.

like image 130
jonsca Avatar answered Oct 05 '22 17:10

jonsca


FindIndex seems to be what you're looking for:

FindIndex(Predicate<T>)

Usage:

list1.FindIndex(x => x==5); 

Example:

// given list1 {3, 4, 6, 5, 7, 8} list1.FindIndex(x => x==5);  // should return 3, as list1[3] == 5; 
like image 20
abelenky Avatar answered Oct 05 '22 18:10

abelenky