English 中文(简体)
c# Array.Find AllIndexOf, FindAll IndexOf
原标题:c# Array.FindAllIndexOf which FindAll IndexOf

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

是否有<代码>Array.Find AllIndexOf?

最佳回答
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].

2. 使其具有延伸方法

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

页: 1

string[] myarr = new string[] {"s", "f", "s"};

int[] v = myarr.FindAllIndexof("s");
问题回答

You can write something like :

string[] someItems = { "cat", "dog", "purple elephant", "unic或n" }; 
var selectedItems = someItems.Select((item, index) => new{
    ItemName = item,
    Position = index});

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

我知道这是一个老的职位,但你可以尝试如下:

string[] cars = {"Volvo", "BMW", "Volvo", "Mazda","BMW","BMW"};
var res = Enumerable.Range(0, cars.Length).Where(i => cars[i] == "BMW").ToList();

∗∗∗∗

寻找一个与特定前提所确定的条件相称的要素,并将整个系统内发生的零基指数归为后。 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();
}

无。 但是,你可以写上自己的extension methods

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

接着,就你们的阵列而言,它就叫了。

页: 1 指数

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

我使用了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;
    }

清单中包含价值,并列出与价值相符的数值。 清单中列有对应指数的分类。

你们只能创造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++;
}

如何简单:

public static IEnumerable<int> Available()
    {
        for (int i = 0; i < myarr.Length; i++)
        {
            if (myarr[i] is null) //Your predicate here...
                yield return i;
        }
    }
// requirement for list
// using System.Collections.Generic;

// Assuming an array of strings
// Example array to search in
string[] array = new string[] { "test1", "test2", "test5", "test1", "test0", "test3" };

// Example string to search for
string search = "test1";

List<int> AllIndexes = new List<int>();
for (int i = 0; i < array.Length; i++)
{
    int f = Array.IndexOf(array[i..], search);
    if (f == -1) { break; }
    AllIndexes.Add(f+i);
    i += f;
}
int[] AllIndexesArray = AllIndexes.ToArray();

由于<条码>IndexOf首次出现,我们可以使用一种简单的<条码>,用于,通过结果加以公布,并将其列入名单。 我认为,直接使用一个阵列也应奏效,但使用一个名单比较容易。 这还取决于你想要与结果做些什么。

This might be more code then other solutions, but depending on what you re doing (example looping through the results), this might be the ideal solution.

注: <([i.]) 存在于DotNet 5 之后。

一项职能可考虑:

public static int[] FindAllIndexOf<T>(T[] array, T value)
{
    List<int> AllIndexes = new List<int>();
    for (int i = 0; i < array.Length; i++)
    {
        int f = Array.IndexOf(array[i..], value);
        if (f == -1) { break; }
        AllIndexes.Add(f+i);
        i += f;
    }
    return AllIndexes.ToArray();
}

或,使用

public static IEnumerable<int> FindAllIndexOf<T>(T[] array, T value)
{
    for (int i = 0; i < array.Length; i++)
    {
        int f = Array.IndexOf(array[i..], value);
        if (f > -1)
            yield return f+i;
        else break;
        i += f;
    }
}

我知道,这个问题已经得到了回答,这只是另一种方式。 注

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签