Trouver l'index d'un élément dans un array dans C#
Cet article explique comment trouver l'index d'un élément dans un array dans C#.
La solution doit soit renvoyer l'index de la première occurrence de l'élément requis, soit -1 s'il n'est pas présent dans le array.
1. Utilisation Array.IndexOf()
méthode
La solution recommandée est d'utiliser le Array.IndexOf() méthode qui renvoie l'index de la première occurrence de l'élément spécifié dans ce array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return Array.IndexOf(array, item); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Résultat: Element 4 is found at index 3 */ |
2. Utilisation Array.FindIndex()
méthode
La Array.FindIndex() La méthode renvoie l'index du premier élément qui satisfait le prédicat fourni, ou -1
s'il n'y a pas un tel élément.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return Array.FindIndex(array, val => val.Equals(item)); } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Résultat: Element 4 is found at index 3 */ |
3. Utilisation Enumerable.Select()
méthode
La System.Linq.Enumerable.Select() La méthode projette chaque élément d'une séquence dans une nouvelle forme. L'exemple de code suivant montre comment nous pouvons utiliser Select()
pour projeter sur une séquence de valeurs, et utilisez à la fois la valeur et l'index de chaque élément pour trouver la première occurrence de l'élément spécifié dans ce array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { try { return array .Select((element, index) => new KeyValuePair<T, int>(element, index)) .First(x => x.Key.Equals(item)).Value; } catch (InvalidOperationException) { return -1; } } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Résultat: Element 4 is found at index 3 */ |
Nous pouvons éviter le bloc try-catch en utilisant FirstOrDefault()
méthode au lieu de First()
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Linq; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { return array .Select((element, index) => new { element, index }) .FirstOrDefault(x => x.element.Equals(item)) ?. index ?? -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Résultat: Element 4 is found at index 3 */ |
4. Effectuer une recherche linéaire
Une solution naïve consiste à effectuer une recherche linéaire sur le array donné pour déterminer si l'élément cible est présent dans le array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; public static class Extensions { public static int findIndex<T>(this T[] array, T item) { EqualityComparer<T> comparer = EqualityComparer<T>.Default; for (int i = 0; i < array.Length; i++) { if (comparer.Equals(array[i], item)) { return i; } } return -1; } } public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int item = 4; int index = array.findIndex(item); if (index != -1) { Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index)); } else { Console.WriteLine("Element not found in the given array."); } } } /* Résultat: Element 4 is found at index 3 */ |
Il s'agit de trouver l'index d'un élément dans un array en C#.