查找多个索引数组 [英] Find multiple index in array

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

问题描述

说我有一个这样的数组

  string [] fruits = {"watermelon","apple","apple","kiwi","pear","banana"};

是否有一个内置的功能,让我查询苹果的所有指标? 例如,

Is there an built in function that allows me to query all the index of "apple" ? For example,

  fruits.FindAllIndex("apple"); 

将返回图1和2的阵列

will return an array of 1 and 2

如果没有的话,我应该怎么实现呢?

If there is not, how should I implement it?

谢谢!

推荐答案

一种方法是这样写:

var indices = fruits
                .Select ((f, i) => new {f, i})
                .Where (x => x.f == "apple")
                .Select (x => x.i);

还是以传统的方式:

Or the traditional way:

var indices = new List<int>();
for (int i = 0; i < fruits.Length; i++)
    if(fruits[i] == "apple")
        indices.Add(i);

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

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