Partager via


Array.IndexOf Méthode

Définition

Recherche l’objet spécifié et retourne l’index de sa première occurrence dans un tableau unidimensionnel ou dans une plage d’éléments du tableau.

Surcharges

Nom Description
IndexOf(Array, Object)

Recherche l’objet spécifié et retourne l’index de sa première occurrence dans un tableau unidimensionnel.

IndexOf(Array, Object, Int32)

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de sa première occurrence. La plage s’étend d’un index spécifié à la fin du tableau.

IndexOf(Array, Object, Int32, Int32)

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de ifs first occurrence. La plage s’étend d’un index spécifié pour un nombre spécifié d’éléments.

IndexOf<T>(T[], T, Int32)

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de sa première occurrence. La plage s’étend d’un index spécifié à la fin du tableau.

IndexOf<T>(T[], T, Int32, Int32)

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de sa première occurrence. La plage s’étend d’un index spécifié pour un nombre spécifié d’éléments.

IndexOf<T>(T[], T)

Recherche l’objet spécifié et retourne l’index de sa première occurrence dans un tableau unidimensionnel.

IndexOf(Array, Object)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Recherche l’objet spécifié et retourne l’index de sa première occurrence dans un tableau unidimensionnel.

public:
 static int IndexOf(Array ^ array, System::Object ^ value);
public static int IndexOf(Array array, object value);
public static int IndexOf(Array array, object? value);
static member IndexOf : Array * obj -> int
Public Shared Function IndexOf (array As Array, value As Object) As Integer

Paramètres

array
Array

Tableau unidimensionnel à rechercher.

value
Object

Objet à localiser dans array.

Retours

Index de la première occurrence de valuearray, s’il est trouvé ; sinon, la limite inférieure du tableau moins 1.

Exceptions

array a la valeur null.

array est multidimensionnel.

Exemples

L’exemple appelle les trois surcharges suivantes de la IndexOf méthode pour rechercher l’index d’une chaîne dans un tableau de chaînes :

  • IndexOf(Array, Object), pour déterminer la première occurrence de la chaîne « the » dans un tableau de chaînes.

  • IndexOf(Array, Object, Int32), pour déterminer la première occurrence de la chaîne « the » dans le quatrième au dernier élément d’un tableau de chaînes.

  • IndexOf(Array, Object, Int32, Int32), pour déterminer la première occurrence de la chaîne « the » dans un tableau de chaînes de l’élément qui suit la dernière correspondance réussie à la fin du tableau.

// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]

// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"

// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."

// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."

// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };

// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);

// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }

      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      Next

      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = Array.IndexOf(strings, searchString)
      Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
                        searchString, index)
        
      ' Search for the first occurrence of the duplicated value in the last section of the array.
      index = Array.IndexOf(strings, searchString, 4)
      Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
                        searchString, index)
        
      ' Search for the first occurrence of the duplicated value in a section of the array.
       Dim position As Integer = index + 1
       index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
       Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
                         searchString, position, strings.GetUpperBound(0), index)
    End Sub
End Module
' The example displays the following output:
'    The array contains the following values:
'       [ 0]: the
'       [ 1]: quick
'       [ 2]: brown
'       [ 3]: fox
'       [ 4]: jumps
'       [ 5]: over
'       [ 6]: the
'       [ 7]: lazy
'       [ 8]: dog
'       [ 9]: in
'       [10]: the
'       [11]: barn
'    The first occurrence of "the" is at index 0.
'    The first occurrence of "the" between index 4 and the end is at index 6.
'    The first occurrence of "the" between index 7 and index 11 is at index 10.

Remarques

Cette méthode recherche tous les éléments d’un tableau unidimensionnel.value Pour déterminer si value elle existe array, la méthode effectue une comparaison d’égalité à l’aide du comparateur EqualityComparer<T>.Defaultd’égalité par défaut.

Étant donné que la plupart des tableaux ont une limite inférieure de zéro, cette méthode retourne généralement -1 sivalue elle n’est pas trouvée. Dans les rares cas où la limite inférieure du tableau est égale à Int32.MinValue(0x80000000) et value n’est pas trouvée, cette méthode retourne Int32.MaxValue (0x7FFFFFFF).

Cette méthode est une opération O(n), où n est le Lengtharray.

Voir aussi

S’applique à

IndexOf(Array, Object, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de sa première occurrence. La plage s’étend d’un index spécifié à la fin du tableau.

public:
 static int IndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int IndexOf(Array array, object value, int startIndex);
public static int IndexOf(Array array, object? value, int startIndex);
static member IndexOf : Array * obj * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer) As Integer

Paramètres

array
Array

Tableau unidimensionnel à rechercher.

value
Object

Objet à localiser dans array.

startIndex
Int32

Index de départ de la recherche. 0 (zéro) est valide dans un tableau vide.

Retours

Index de la première occurrence de value, s’il est trouvé, dans la plage d’éléments dans array laquelle s’étend le startIndex dernier élément ; sinon, la limite inférieure du tableau moins 1.

Exceptions

array a la valeur null.

startIndex est en dehors de la plage d’index valides pour array.

array est multidimensionnel.

Exemples

L’exemple appelle les trois surcharges suivantes de la IndexOf méthode pour rechercher l’index d’une chaîne dans un tableau de chaînes :

  • IndexOf(Array, Object), pour déterminer la première occurrence de la chaîne « the » dans un tableau de chaînes.

  • IndexOf(Array, Object, Int32), pour déterminer la première occurrence de la chaîne « the » dans le quatrième au dernier élément d’un tableau de chaînes.

  • IndexOf(Array, Object, Int32, Int32), pour déterminer la première occurrence de la chaîne « the » dans un tableau de chaînes de l’élément qui suit la dernière correspondance réussie à la fin du tableau.

// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]

// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"

// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."

// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."

// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };

// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);

// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }

      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      Next

      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = Array.IndexOf(strings, searchString)
      Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
                        searchString, index)
        
      ' Search for the first occurrence of the duplicated value in the last section of the array.
      index = Array.IndexOf(strings, searchString, 4)
      Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
                        searchString, index)
        
      ' Search for the first occurrence of the duplicated value in a section of the array.
       Dim position As Integer = index + 1
       index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
       Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
                         searchString, position, strings.GetUpperBound(0), index)
    End Sub
End Module
' The example displays the following output:
'    The array contains the following values:
'       [ 0]: the
'       [ 1]: quick
'       [ 2]: brown
'       [ 3]: fox
'       [ 4]: jumps
'       [ 5]: over
'       [ 6]: the
'       [ 7]: lazy
'       [ 8]: dog
'       [ 9]: in
'       [10]: the
'       [11]: barn
'    The first occurrence of "the" is at index 0.
'    The first occurrence of "the" between index 4 and the end is at index 6.
'    The first occurrence of "the" between index 7 and index 11 is at index 10.

Remarques

Cette méthode recherche un tableau unidimensionnel de l’élément à l’index startIndex vers le dernier élément. Pour déterminer si value elle existe array, la méthode effectue une comparaison d’égalité à l’aide du comparateur EqualityComparer<T>.Defaultd’égalité par défaut.

Étant donné que la plupart des tableaux ont une limite inférieure de zéro, cette méthode retourne généralement -1 si value elle n’est pas trouvée. Dans les rares cas où la limite inférieure du tableau est égale à Int32.MinValue(0x80000000) et value n’est pas trouvée, cette méthode retourne Int32.MaxValue (0x7FFFFFFF).

Si startIndex elle est Array.Lengthégale, la méthode retourne -1. Si startIndex elle est supérieure Array.Lengthà , la méthode lève un ArgumentOutOfRangeException.

Cette méthode est une opération O(n), où n est le nombre d’éléments de startIndex la fin de array.

Voir aussi

S’applique à

IndexOf(Array, Object, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de ifs first occurrence. La plage s’étend d’un index spécifié pour un nombre spécifié d’éléments.

public:
 static int IndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int IndexOf(Array array, object value, int startIndex, int count);
public static int IndexOf(Array array, object? value, int startIndex, int count);
static member IndexOf : Array * obj * int * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer

Paramètres

array
Array

Tableau unidimensionnel à rechercher.

value
Object

Objet à localiser dans array.

startIndex
Int32

Index de départ de la recherche. 0 (zéro) est valide dans un tableau vide.

count
Int32

Nombre d’éléments à rechercher.

Retours

Index de la première occurrence de value, s’il est trouvé dans l’index arraystartIndex à startIndex + count - 1 ; sinon, la limite inférieure du tableau moins 1.

Exceptions

array a la valeur null.

startIndex est en dehors de la plage d’index valides pour array.

- ou -

count est inférieur à zéro.

- ou -

startIndex et count ne spécifiez pas de section valide dans array.

array est multidimensionnel.

Exemples

L’exemple appelle les trois surcharges suivantes de la IndexOf méthode pour rechercher l’index d’une chaîne dans un tableau de chaînes :

  • IndexOf(Array, Object), pour déterminer la première occurrence de la chaîne « the » dans un tableau de chaînes.

  • IndexOf(Array, Object, Int32), pour déterminer la première occurrence de la chaîne « the » dans le quatrième au dernier élément d’un tableau de chaînes.

  • IndexOf(Array, Object, Int32, Int32), pour déterminer la première occurrence de la chaîne « the » dans un tableau de chaînes de l’élément qui suit la dernière correspondance réussie à la fin du tableau. Pour déterminer la valeur de l’argument count , elle soustrait la limite supérieure du tableau de l’index de départ et en ajoute une.

// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]

// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"

// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."

// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."

// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
                     "over", "the", "lazy", "dog", "in", "the",
                     "barn" };

// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
   Console.WriteLine("   [{0,2}]: {1}", i, strings[i]);

// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
                  searchString, index);

// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
                  searchString, position, strings.GetUpperBound(0), index);

// The example displays the following output:
//    The array contains the following values:
//       [ 0]: the
//       [ 1]: quick
//       [ 2]: brown
//       [ 3]: fox
//       [ 4]: jumps
//       [ 5]: over
//       [ 6]: the
//       [ 7]: lazy
//       [ 8]: dog
//       [ 9]: in
//       [10]: the
//       [11]: barn
//    The first occurrence of "the" is at index 0.
//    The first occurrence of "the" between index 4 and the end is at index 6.
//    The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }

      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      Next

      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = Array.IndexOf(strings, searchString)
      Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
                        searchString, index)
        
      ' Search for the first occurrence of the duplicated value in the last section of the array.
      index = Array.IndexOf(strings, searchString, 4)
      Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
                        searchString, index)
        
      ' Search for the first occurrence of the duplicated value in a section of the array.
       Dim position As Integer = index + 1
       index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
       Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
                         searchString, position, strings.GetUpperBound(0), index)
    End Sub
End Module
' The example displays the following output:
'    The array contains the following values:
'       [ 0]: the
'       [ 1]: quick
'       [ 2]: brown
'       [ 3]: fox
'       [ 4]: jumps
'       [ 5]: over
'       [ 6]: the
'       [ 7]: lazy
'       [ 8]: dog
'       [ 9]: in
'       [10]: the
'       [11]: barn
'    The first occurrence of "the" is at index 0.
'    The first occurrence of "the" between index 4 and the end is at index 6.
'    The first occurrence of "the" between index 7 and index 11 is at index 10.

Remarques

Cette méthode recherche les éléments d’un tableau unidimensionnel de startIndex à plus count moins 1, s’il count est supérieur à startIndex 0. Pour déterminer si value elle existe array, la méthode effectue une comparaison d’égalité à l’aide du comparateur EqualityComparer<T>.Defaultd’égalité par défaut.

Étant donné que la plupart des tableaux ont une limite inférieure de zéro, cette méthode retourne généralement -1 quand value elle est introuvable. Dans les rares cas où la limite inférieure du tableau est égale à Int32.MinValue (0x80000000) et value n’est pas trouvée, cette méthode retourne Int32.MaxValue (0x7FFFFFFF).

Si startindex elle est Array.Lengthégale, la méthode retourne -1. Si startIndex elle est supérieure Array.Lengthà , la méthode lève un ArgumentOutOfRangeException.

Cette méthode est une opération O(n), où n est count.

Voir aussi

S’applique à

IndexOf<T>(T[], T, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de sa première occurrence. La plage s’étend d’un index spécifié à la fin du tableau.

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int IndexOf<T>(T[] array, T value, int startIndex);
static member IndexOf : 'T[] * 'T * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer

Paramètres de type

T

Type des éléments du tableau.

Paramètres

array
T[]

Tableau unidimensionnel basé sur zéro à rechercher.

value
T

Objet à localiser dans array.

startIndex
Int32

Index de départ de base zéro de la recherche. 0 (zéro) est valide dans un tableau vide.

Retours

Index de base zéro de la première occurrence de value la plage d’éléments dans array laquelle s’étend de startIndex jusqu’au dernier élément, s’il est trouvé ; sinon, -1.

Exceptions

array a la valeur null.

startIndex est en dehors de la plage d’index valides pour array.

Exemples

L’exemple suivant illustre les trois surcharges génériques de la IndexOf méthode. Un tableau de chaînes est créé, avec une entrée qui apparaît deux fois, à l’emplacement d’index 0 et à l’emplacement d’index 5. La IndexOf<T>(T[], T) surcharge de méthode recherche le tableau à partir du début et recherche la première occurrence de la chaîne. La IndexOf<T>(T[], T, Int32) surcharge de méthode est utilisée pour rechercher le tableau à partir de l’emplacement d’index 3 et passer à la fin du tableau, et recherche la deuxième occurrence de la chaîne. Enfin, la IndexOf<T>(T[], T, Int32, Int32) surcharge de méthode est utilisée pour rechercher une plage de deux entrées, en commençant à l’emplacement d’index 2 . Elle retourne -1 car il n’existe aucune instance de la chaîne de recherche dans cette plage.

string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System

let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"

// This code example produces the following output:
//
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1

Remarques

Cette méthode recherche un tableau unidimensionnel de l’élément à startIndex la fin du tableau. Pour déterminer si value elle existe array, la méthode effectue une comparaison d’égalité à l’aide du comparateur EqualityComparer<T>.Defaultd’égalité par défaut.

Si startIndex elle est Lengthégale, la méthode retourne -1. Si startIndex elle est supérieure Array.Lengthà , la méthode lève un ArgumentOutOfRangeException.

Cette méthode est une opération O(n), où n est le nombre d’éléments de startIndex la fin de array.

Voir aussi

S’applique à

IndexOf<T>(T[], T, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Recherche l’objet spécifié dans une plage d’éléments d’un tableau unidimensionnel et retourne l’index de sa première occurrence. La plage s’étend d’un index spécifié pour un nombre spécifié d’éléments.

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int IndexOf<T>(T[] array, T value, int startIndex, int count);
static member IndexOf : 'T[] * 'T * int * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer, count As Integer) As Integer

Paramètres de type

T

Type des éléments du tableau.

Paramètres

array
T[]

Tableau unidimensionnel basé sur zéro à rechercher.

value
T

Objet à localiser dans array.

startIndex
Int32

Index de départ de base zéro de la recherche. 0 (zéro) est valide dans un tableau vide.

count
Int32

Nombre d’éléments de la section à rechercher.

Retours

Index de base zéro de la première occurrence de value la plage d’éléments dans array laquelle commence startIndex et contient le nombre d’éléments spécifiés dans count, s’il est trouvé ; sinon, -1.

Exceptions

array a la valeur null.

startIndex est en dehors de la plage d’index valides pour array.

- ou -

count est inférieur à zéro.

- ou -

startIndex et count ne spécifiez pas de section valide dans array.

Exemples

L’exemple suivant illustre les trois surcharges génériques de la IndexOf méthode. Un tableau de chaînes est créé, avec une entrée qui apparaît deux fois, à l’emplacement d’index 0 et à l’emplacement d’index 5. La IndexOf<T>(T[], T) surcharge de méthode recherche le tableau à partir du début et recherche la première occurrence de la chaîne. La IndexOf<T>(T[], T, Int32) surcharge de méthode est utilisée pour rechercher le tableau à partir de l’emplacement d’index 3 et passer à la fin du tableau, et recherche la deuxième occurrence de la chaîne. Enfin, la IndexOf<T>(T[], T, Int32, Int32) surcharge de méthode est utilisée pour rechercher une plage de deux entrées, en commençant à l’emplacement d’index 2 . Elle retourne -1 car il n’existe aucune instance de la chaîne de recherche dans cette plage.

string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System

let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"

// This code example produces the following output:
//
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1

Remarques

Cette méthode recherche les éléments d’un tableau unidimensionnel de startIndex à plus count moins 1, s’il count est supérieur à startIndex 0. Pour déterminer si value elle existe array, la méthode effectue une comparaison d’égalité à l’aide du comparateur EqualityComparer<T>.Defaultd’égalité par défaut.

Si startIndex elle est Array.Lengthégale, la méthode retourne -1. Si startIndex elle est supérieure Array.Lengthà , la méthode lève un ArgumentOutOfRangeException.

Cette méthode est une opération O(n), où n est count.

Voir aussi

S’applique à

IndexOf<T>(T[], T)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

Recherche l’objet spécifié et retourne l’index de sa première occurrence dans un tableau unidimensionnel.

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value);
public static int IndexOf<T>(T[] array, T value);
static member IndexOf : 'T[] * 'T -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T) As Integer

Paramètres de type

T

Type des éléments du tableau.

Paramètres

array
T[]

Tableau unidimensionnel basé sur zéro à rechercher.

value
T

Objet à localiser dans array.

Retours

Index de base zéro de la première occurrence de value l’ensemble array, s’il est trouvé ; sinon, -1.

Exceptions

array a la valeur null.

Exemples

L’exemple suivant illustre les trois surcharges génériques de la IndexOf méthode. Un tableau de chaînes est créé, avec une entrée qui apparaît deux fois, à l’emplacement d’index 0 et à l’emplacement d’index 5. La IndexOf<T>(T[], T) surcharge de méthode recherche le tableau à partir du début et recherche la première occurrence de la chaîne. La IndexOf<T>(T[], T, Int32) surcharge de méthode est utilisée pour rechercher le tableau à partir de l’emplacement d’index 3 et passer à la fin du tableau, et recherche la deuxième occurrence de la chaîne. Enfin, la IndexOf<T>(T[], T, Int32, Int32) surcharge de méthode est utilisée pour rechercher une plage de deux entrées, en commençant à l’emplacement d’index 2 . Elle retourne -1 car il n’existe aucune instance de la chaîne de recherche dans cette plage.

string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System

let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"

// This code example produces the following output:
//
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
//    Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1

Remarques

Cette méthode recherche tous les éléments d’un tableau unidimensionnel.value Pour déterminer si value elle existe array, la méthode effectue une comparaison d’égalité à l’aide du comparateur EqualityComparer<T>.Defaultd’égalité par défaut.

Cette méthode est une opération O(n), où n est le Lengtharray.

Voir aussi

S’applique à