Sé que C# tiene Array.FindAll
y Array.IndexOf
.C# Array.FindAllIndexOf which FindAll IndexOf
¿Hay un Array.FindAllIndexOf
que devuelve int[]
?
Sé que C# tiene Array.FindAll
y Array.IndexOf
.C# Array.FindAllIndexOf which FindAll IndexOf
¿Hay un Array.FindAllIndexOf
que devuelve int[]
?
string[] myarr = new string[] {"s", "f", "s"};
int[] v = myarr.Select((b,i) => b == "s" ? i : -1).Where(i => i != -1).ToArray();
Esto devolverá 0, 2
Si el valor no existe en la matriz, devolverá un int [0].
crea un método de extensión de la misma
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();
}
}
y lo llaman como
string[] myarr = new string[] {"s", "f", "s"};
int[] v = myarr.FindAllIndexof("s");
Puede escribir algo como:
string[] someItems = { "cat", "dog", "purple elephant", "unicorn" };
var selectedItems = someItems.Select((item, index) => new{
ItemName = item,
Position = index});
o
var Items = someItems.Select((item, index) => new{
ItemName = item,
Position = index}).Where(i => i.ItemName == "purple elephant");
puede recorrer con findIndex dando un índice
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);
No, no lo hay. Pero puede escribir su propio 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();
}
y luego, para su matriz, llámelo.
Busca un elemento que coincida con las condiciones definidas por el predicado especificado, y devuelve todo el índice basado en cero de la ocurrencia dentro de System.Array completo.
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();
}
Soy consciente de que la pregunta ya está respondida, esta es solo otra forma de hacerlo. en cuenta que he utilizado en lugar de 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++;
}
He usado la respuesta de Nikhil Agrawal para crear el siguiente método relacionado, que puede ser útil.
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;
}
Que lleva una lista con los valores y una lista con los valores que deben coincidir. Devuelve una lista de enteros con el índice de las coincidencias.
Forma más corta - 'Enumerable.Range (0, myarr.Length) .Where (i => myarr [i] ==" s "). ToArray();' – diesel
@NikhilAgrawal ¿Cómo podría ser modificado para encontrar todos índices de números de punto flotante iguales (dentro de la precisión) a un valor especificado? – gwizardry