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.
Use the . IndexOf() method of the list. Specs for the method can be found on MSDN. Show activity on this post.
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.
The index() method returns the index of the given element in the list. If the element is not found, a ValueError exception is raised.
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.
Use the .IndexOf()
method of the list. Specs for the method can be found on MSDN.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With