查找数组中某个值的索引 [英] Find index of a value in an array

查看:23
本文介绍了查找数组中某个值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

linq 可以以某种方式用于在数组中查找值的索引吗?

Can linq somehow be used to find the index of a value in an array?

例如,这个循环定位数组中的键索引.

For instance, this loop locates the key index within an array.

for (int i = 0; i < words.Length; i++)
{
    if (words[i].IsKey)
    {
        keyIndex = i;
    }
}

推荐答案

int keyIndex = Array.FindIndex(words, w => w.IsKey);

这实际上为您提供了整数索引而不是对象,无论您创建了什么自定义类

That actually gets you the integer index and not the object, regardless of what custom class you have created

这篇关于查找数组中某个值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆