c# Array.FindAllIndexOf 其中FindAll IndexOf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10443461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# Array.FindAllIndexOf which FindAll IndexOf
提问by Eric Yin
I know c# has Array.FindAlland Array.IndexOf.
我知道 c# 有Array.FindAll和Array.IndexOf.
Is there a Array.FindAllIndexOfwhich returns int[]?
是否有Array.FindAllIndexOf返回int[]?
采纳答案by Nikhil Agrawal
string[] myarr = new string[] {"s", "f", "s"};
int[] v = myarr.Select((b,i) => b == "s" ? i : -1).Where(i => i != -1).ToArray();
This will return 0, 2
这将返回 0, 2
If the value does not exist in the array then it will return a int[0].
如果数组中不存在该值,则它将返回一个 int[0]。
make an extension method of it
对其进行扩展方法
public static class EM
{
public static int[] FindAllIndexof<T>(this IEnumerable<T> values, T val)
{
return values.Select((b,i) => object.Equals(b, val) ? i : -1).Where(i => i != -1).ToArray();
}
}
and call it like
并称之为
string[] myarr = new string[] {"s", "f", "s"};
int[] v = myarr.FindAllIndexof("s");
回答by Pranay Rana
You can write something like :
你可以这样写:
string[] someItems = { "cat", "dog", "purple elephant", "unicorn" };
var selectedItems = someItems.Select((item, index) => new{
ItemName = item,
Position = index});
or
或者
var Items = someItems.Select((item, index) => new{
ItemName = item,
Position = index}).Where(i => i.ItemName == "purple elephant");
回答by msam
回答by stukselbax
No, there is not. But you can write your own extension method.
不,那里没有。但是您可以编写自己的扩展方法。
public static int[] FindAllIndexOf<T>(this T[] a, Predicate<T> match)
{
T[] subArray = Array.FindAll<T>(a, match);
return (from T item in subArray select Array.IndexOf(a, item)).ToArray();
}
and then, for your array, call it.
然后,对于您的数组,调用它。
回答by Rocket
Searches for an element that matches the conditions defined by the specified predicate, and returns all the zero-based index of the occurrence within the entire System.Array.
搜索与指定谓词定义的条件匹配的元素,并返回整个 System.Array 中出现的所有从零开始的索引。
public static int[] FindAllIndex<T>(this T[] array, Predicate<T> match)
{
return array.Select((value, index) => match(value) ? index : -1)
.Where(index => index != -1).ToArray();
}
回答by Sirwan Esfehani
I'm aware that the question is answered already, this is just another way of doing it. note that I used ArrayListinstead of int[]
我知道这个问题已经回答了,这只是另一种方式。请注意,我使用ArrayList而不是int[]
// required using directives
using System;
using System.Collections;
String inputString = "The lazy fox couldn't jump, poor fox!";
ArrayList locations = new ArrayList(); // array for found indexes
string[] lineArray = inputString.Split(' '); // inputString to array of strings separated by spaces
int tempInt = 0;
foreach (string element in lineArray)
{
if (element == "fox")
{
locations.Add(tempInt); // tempInt will be the index of current found index and added to Arraylist for further processing
}
tempInt++;
}
回答by Henrik Larsen
I've used Nikhil Agrawal's answer to create the following related method, which may be useful.
我已经使用 Nikhil Agrawal 的回答创建了以下相关方法,这可能很有用。
public static List<int> FindAllIndexOf<T>(List<T> values, List<T> matches)
{
// Initialize list
List<int> index = new List<int>();
// For each value in matches get the index and add to the list with indexes
foreach (var match in matches)
{
// Find matches
index.AddRange(values.Select((b, i) => Equals(b, match) ? i : -1).Where(i => i != -1).ToList());
}
return index;
}
Which takes a list with values and a list with values that are to be matched. It returns a list of integers with the index of the matches.
它需要一个包含值的列表和一个包含要匹配的值的列表。它返回一个带有匹配索引的整数列表。
回答by Fatih Kaan A?IKG?Z
You can solve this problem by creating only 2 integer variables. More power to you!
您可以通过仅创建 2 个整数变量来解决此问题。给你更多的力量!
string[] seasons= { "Fall","Spring", "Summer", "Fall", "Fall", "Winter"};
int i = 0;
int IndexOfFallInArray = 0;
int[] IndexesOfFall= new int[seasons.Length];
foreach (var item in seasons)
{
if (item == "Fall")
{
IndexesOfFall[i] = IndexOfFallInArray;
i++;
}
IndexOfFallInArray++;
}
回答by Furkan ?ztürk
I know this is an old post, but you can try the following,
我知道这是一个旧帖子,但您可以尝试以下操作,
string[] cars = {"Volvo", "BMW", "Volvo", "Mazda","BMW","BMW"};
var res = Enumerable.Range(0, cars.Length).Where(i => cars[i] == "BMW").ToList();
returns {1,4,5}as a list
以列表形式返回{1,4,5}

