在数组中查找一个值的指数 [英] Find index of a value in an array

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

问题描述

能否LINQ某种方式被用于查找值的索引数组中的?

例如,这个循环查找数组中的关键指标。

 的for(int i = 0; I< words.Length;我++)
{
    如果(字[I] .IsKey)
    {
        keyIndex = I;
    }
}


解决方案

  INT keyIndex = Array.FindIndex(即W => w.IsKey);

这实际上是让你的整数索引,而不是对象,不管你创造了什么自定义类的

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天全站免登陆