简体   繁体   English

如何在List中找到元素的索引 <int> ?

[英]How can I find the index of an element inside of my List<int>?

Here's what I have: 这就是我所拥有的:

public int FindItem(int elementoABuscar)
{
    int indice = vectorNumeros.FindIndex(0, asd);
    return indice;
}

I can't seem to figure out the second part of the FindIndex method. 我似乎无法弄清楚FindIndex方法的第二部分。

How would I find the index of elementoABuscar? 我怎样才能找到elementoABuscar的索引?

Just use List<int>.IndexOf : 只需使用List<int>.IndexOf

int indice = vectorNumeros.IndexOf(elementoABuscar);

FindIndex is used to find an item which matches a particular predicate - you don't need it here, if you're just trying to find an element directly. FindIndex用于查找与特定谓词匹配的项目 - 如果您只是想直接查找元素,则此处不需要它。

Note that if your list is sorted, you could also use List<T>.BinarySearch . 请注意,如果您的列表已排序,您还可以使用List<T>.BinarySearch

你为什么不使用list.IndexOf(object)

Try 尝试

int indice = vectorNumeros.IndexOf(elementoABuscar); 

msdn reference msdn参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM