2012-05-04 22 views

Odpowiedz

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

int[] v = myarr.Select((b,i) => b == "s" ? i : -1).Where(i => i != -1).ToArray(); 

ten powróci 0, 2

Jeśli wartość nie istnieje w tablicy, zwróci ona wartość int [0].

uczynić metodę rozszerzenia go

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

i nazwać jak

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

int[] v = myarr.FindAllIndexof("s"); 
+2

Krótszy sposób - 'Enumerable.Range (0, myarr.Length) .Where (i => myarr [i] ==" s.) ToArray(); ' – diesel

+0

@NikhilAgrawal Jak można to zmodyfikować, aby znaleźć wszystkie indeksy liczb zmiennoprzecinkowych równe (w ramach precyzji) do określonej wartości? – gwizardry

6

można napisać coś takiego:

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

lub

var Items = someItems.Select((item, index) => new{ 
    ItemName = item, 
    Position = index}).Where(i => i.ItemName == "purple elephant"); 

Czytaj: Get the index of a given item using LINQ

1

Można pętla z findIndex podając indeks

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

Nie, nie ma. Ale możesz napisać własną 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(); 
} 

, a następnie, dla swojej tablicy, zadzwoń.

3

Wyszukuje element, który pasuje do warunków zdefiniowanych przez określony predykat, i zwraca cały indeks zera dla wystąpienia w obrębie całego obiektu 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(); 
} 
-2

Jestem świadomy, że pytanie jest już odpowiedź, to jest po prostu inny sposób to zrobić. pamiętać, że użyłem ArrayList zamiast 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++; 
} 
0

Użyłem odpowiedź Nikhil Agrawal, aby utworzyć następujący pokrewne metody, które mogą być użyteczne.

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

Który pobiera listę z wartościami i listę z wartościami, które mają zostać dopasowane. Zwraca listę liczb całkowitych z indeksem dopasowań.