c# Array.FindAllIndexOf which FindAll IndexOf

隐身守侯 提交于 2019-11-26 14:12:34

问题


I know c# has Array.FindAll and Array.IndexOf.

Is there a Array.FindAllIndexOf which returns int[]?


回答1:


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

If the value does not exist in the array then it will return a 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");



回答2:


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");

Read : Get the index of a given item using LINQ




回答3:


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.

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();
}



回答4:


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.




回答5:


You can loop with findIndex giving an index

string[] arr = { "abc", "asd", "def", "abc", "lmn", "wer" };
int index = -1;
do
{
    index = Array.IndexOf(arr, "abc", index + 1);
    System.Console.WriteLine(index);
} while (-1 != index);



回答6:


I've used Nikhil Agrawal's answer to create the following related method, which may be useful.

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.




回答7:


You can solve this problem by creating only 2 integer variables. More power to you!

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++;
}



回答8:


I'm aware that the question is answered already, this is just another way of doing it. note that I used ArrayList instead of 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++;
}


来源:https://stackoverflow.com/questions/10443461/c-sharp-array-findallindexof-which-findall-indexof

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!