Sdílet prostřednictvím


Array.IndexOf Metoda

Definice

Vyhledá zadaný objekt a vrátí index prvního výskytu v jednorozměrném poli nebo v oblasti prvků v poli.

Přetížení

Name Description
IndexOf(Array, Object)

Vyhledá zadaný objekt a vrátí index prvního výskytu v jednorozměrném poli.

IndexOf(Array, Object, Int32)

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole.

IndexOf(Array, Object, Int32, Int32)

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu ifs. Rozsah se rozšiřuje ze zadaného indexu pro zadaný počet prvků.

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

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole.

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

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu. Rozsah se rozšiřuje ze zadaného indexu pro zadaný počet prvků.

IndexOf<T>(T[], T)

Vyhledá zadaný objekt a vrátí index prvního výskytu v jednorozměrném poli.

IndexOf(Array, Object)

Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs

Vyhledá zadaný objekt a vrátí index prvního výskytu v jednorozměrném poli.

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

Parametry

array
Array

Jednorozměrné pole, které se má prohledávat.

value
Object

Objekt, ve arraykterý chcete najít .

Návraty

Index prvního výskytu v arrayargumentu value , pokud je nalezen; jinak dolní mez matice minus 1.

Výjimky

array je null.

array je multidimenzionální.

Příklady

Příklad volá následující tři přetížení IndexOf metody k vyhledání indexu řetězce v řetězcovém poli:

// 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.

Poznámky

Tato metoda hledá všechny prvky jednorozměrného pole pro value. Chcete-li zjistit, zda value existuje array, metoda provádí porovnání rovnosti pomocí výchozího porovnávače EqualityComparer<T>.Defaultrovnosti .

Vzhledem k tomu, že většina polí má dolní mez nuly, tato metoda obvykle vrátí -1 pokudvalue se nenajde. Ve výjimečných případech, kdy je dolní mez pole rovna Int32.MinValue(0x80000000) a value nenalezena, tato metoda vrátí Int32.MaxValue (0x7FFFFFFF).

Tato metoda je operace O(n), kde n je Length .array

Viz také

Platí pro

IndexOf(Array, Object, Int32)

Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole.

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

Parametry

array
Array

Jednorozměrné pole, které se má prohledávat.

value
Object

Objekt, ve arraykterý chcete najít .

startIndex
Int32

Počáteční index vyhledávání. Hodnota 0 (nula) je platná v prázdném poli.

Návraty

Index prvníhovýskythochhoch valuearraystartIndex

Výjimky

array je null.

startIndex je mimo rozsah platných indexů pro array.

array je multidimenzionální.

Příklady

Příklad volá následující tři přetížení IndexOf metody k vyhledání indexu řetězce v řetězcovém poli:

// 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.

Poznámky

Tato metoda prohledá jednorozměrné pole z elementu na indexu startIndex do posledního prvku. Chcete-li zjistit, zda value existuje array, metoda provádí porovnání rovnosti pomocí výchozího porovnávače EqualityComparer<T>.Defaultrovnosti .

Vzhledem k tomu, že většina polí má dolní mez nuly, tato metoda obvykle vrátí -1 pokud value se nenajde. Ve výjimečných případech, kdy je dolní mez pole rovna Int32.MinValue(0x80000000) a value nenalezena, tato metoda vrátí Int32.MaxValue (0x7FFFFFFF).

Pokud startIndex se rovná Array.Length, vrátí metoda -1. Pokud startIndex je větší než Array.Length, metoda vyvolá ArgumentOutOfRangeException.

Tato metoda je operace O(n), kde n je počet prvků od startIndex konce array.

Viz také

Platí pro

IndexOf(Array, Object, Int32, Int32)

Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu ifs. Rozsah se rozšiřuje ze zadaného indexu pro zadaný počet prvků.

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

Parametry

array
Array

Jednorozměrné pole, které se má prohledávat.

value
Object

Objekt, ve arraykterý chcete najít .

startIndex
Int32

Počáteční index vyhledávání. Hodnota 0 (nula) je platná v prázdném poli.

count
Int32

Počet prvků, které se mají prohledávat.

Návraty

Index prvního výskytu value, pokud je nalezen v indexucountarraystartIndexstartIndex + do - 1; jinak dolní mez matice minus 1.

Výjimky

array je null.

startIndex je mimo rozsah platných indexů pro array.

nebo

Hodnota count je menší než nula.

nebo

startIndex a count nezadávejte platný oddíl v arraysouboru .

array je multidimenzionální.

Příklady

Příklad volá následující tři přetížení IndexOf metody k vyhledání indexu řetězce v řetězcovém poli:

  • IndexOf(Array, Object), určit první výskyt řetězce "the" v řetězcovém poli.

  • IndexOf(Array, Object, Int32), pro určení prvního výskytu řetězce "the" ve čtvrtém až posledním prvku pole řetězce.

  • IndexOf(Array, Object, Int32, Int32), chcete-li určit první výskyt řetězce "the" v řetězcovém poli z elementu, který následuje za poslední úspěšnou shodu na konci pole. Chcete-li zjistit hodnotu argumentu count , odečte horní mez pole od počátečního indexu a přidá jednu.

// 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.

Poznámky

Tato metoda hledá prvky jednorozměrného pole od startIndex do startIndex plus count 1, pokud count je větší než 0. Chcete-li zjistit, zda value existuje array, metoda provádí porovnání rovnosti pomocí výchozího porovnávače EqualityComparer<T>.Defaultrovnosti .

Vzhledem k tomu, že většina polí má dolní mez nuly, tato metoda obecně vrací -1, pokud value se nenajde. Ve výjimečných případech, kdy je dolní mez pole rovna Int32.MinValue (0x80000000) a value nenalezena, tato metoda vrátí Int32.MaxValue (0x7FFFFFFF).

Pokud startindex se rovná Array.Length, metoda vrátí -1. Pokud startIndex je větší než Array.Length, metoda vyvolá ArgumentOutOfRangeException.

Tato metoda je operace O(n), kde n je count.

Viz také

Platí pro

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

Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole.

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

Parametry typu

T

Typ prvků pole.

Parametry

array
T[]

Jednorozměrné pole založené na nule, které se má prohledávat.

value
T

Objekt, ve arraykterý chcete najít .

startIndex
Int32

Počáteční index vyhledávání založený na nule. Hodnota 0 (nula) je platná v prázdném poli.

Návraty

Index založený na nule prvního výskytu value v rozsahu prvků, array který se vztahuje od startIndex posledního prvku, pokud je nalezen; jinak -1.

Výjimky

array je null.

startIndex je mimo rozsah platných indexů pro array.

Příklady

Následující příklad ukazuje všechny tři obecné přetížení IndexOf metody. Vytvoří se pole řetězců s jednou položkou, která se zobrazí dvakrát, v umístění indexu 0 a umístění indexu 5. Přetížení IndexOf<T>(T[], T) metody prohledá pole od začátku a najde první výskyt řetězce. Přetížení IndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole počínaje umístěním indexu 3 a pokračuje na konec pole a najde druhý výskyt řetězce. IndexOf<T>(T[], T, Int32, Int32) Nakonec se přetížení metody používá k prohledání rozsahu dvou položek, počínaje umístěním indexu 2; vrátí -1, protože v tomto rozsahu neexistují žádné instance vyhledávacího řetězce.

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

Poznámky

Tato metoda hledá jednorozměrné pole z elementu na startIndex konci pole. Chcete-li zjistit, zda value existuje array, metoda provádí porovnání rovnosti pomocí výchozího porovnávače EqualityComparer<T>.Defaultrovnosti .

Pokud startIndex se rovná Length, vrátí metoda -1. Pokud startIndex je větší než Array.Length, metoda vyvolá ArgumentOutOfRangeException.

Tato metoda je operace O(n), kde n je počet prvků od startIndex konce array.

Viz také

Platí pro

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

Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs

Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu. Rozsah se rozšiřuje ze zadaného indexu pro zadaný počet prvků.

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

Parametry typu

T

Typ prvků pole.

Parametry

array
T[]

Jednorozměrné pole založené na nule, které se má prohledávat.

value
T

Objekt, ve arraykterý chcete najít .

startIndex
Int32

Počáteční index vyhledávání založený na nule. Hodnota 0 (nula) je platná v prázdném poli.

count
Int32

Počet prvků v oddílu, které se mají prohledávat.

Návraty

Index založený na nule prvního výskytu value v rozsahu prvků, array které začínají startIndex a obsahuje počet prvků zadaných v countpřípadě nalezení; jinak -1.

Výjimky

array je null.

startIndex je mimo rozsah platných indexů pro array.

nebo

Hodnota count je menší než nula.

nebo

startIndex a count nezadávejte platný oddíl v arraysouboru .

Příklady

Následující příklad ukazuje všechny tři obecné přetížení IndexOf metody. Vytvoří se pole řetězců s jednou položkou, která se zobrazí dvakrát, v umístění indexu 0 a umístění indexu 5. Přetížení IndexOf<T>(T[], T) metody prohledá pole od začátku a najde první výskyt řetězce. Přetížení IndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole počínaje umístěním indexu 3 a pokračuje na konec pole a najde druhý výskyt řetězce. IndexOf<T>(T[], T, Int32, Int32) Nakonec se přetížení metody používá k prohledání rozsahu dvou položek, počínaje umístěním indexu 2; vrátí -1, protože v tomto rozsahu neexistují žádné instance vyhledávacího řetězce.

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

Poznámky

Tato metoda hledá prvky jednorozměrného pole od startIndex do startIndex plus count 1, pokud count je větší než 0. Chcete-li zjistit, zda value existuje array, metoda provádí porovnání rovnosti pomocí výchozího porovnávače EqualityComparer<T>.Defaultrovnosti .

Pokud startIndex se rovná Array.Length, metoda vrátí -1. Pokud startIndex je větší než Array.Length, metoda vyvolá ArgumentOutOfRangeException.

Tato metoda je operace O(n), kde n je count.

Viz také

Platí pro

IndexOf<T>(T[], T)

Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs
Zdroj:
Array.cs

Vyhledá zadaný objekt a vrátí index prvního výskytu v jednorozměrném poli.

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

Parametry typu

T

Typ prvků pole.

Parametry

array
T[]

Jednorozměrné pole založené na nule, které se má prohledávat.

value
T

Objekt, ve arraykterý chcete najít .

Návraty

Index založený na nule prvního výskytu value v celém arrayobjektu , pokud je nalezen; v opačném případě -1.

Výjimky

array je null.

Příklady

Následující příklad ukazuje všechny tři obecné přetížení IndexOf metody. Vytvoří se pole řetězců s jednou položkou, která se zobrazí dvakrát, v umístění indexu 0 a umístění indexu 5. Přetížení IndexOf<T>(T[], T) metody prohledá pole od začátku a najde první výskyt řetězce. Přetížení IndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole počínaje umístěním indexu 3 a pokračuje na konec pole a najde druhý výskyt řetězce. IndexOf<T>(T[], T, Int32, Int32) Nakonec se přetížení metody používá k prohledání rozsahu dvou položek, počínaje umístěním indexu 2; vrátí -1, protože v tomto rozsahu neexistují žádné instance vyhledávacího řetězce.

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

Poznámky

Tato metoda hledá všechny prvky jednorozměrného pole pro value. Chcete-li zjistit, zda value existuje array, metoda provádí porovnání rovnosti pomocí výchozího porovnávače EqualityComparer<T>.Defaultrovnosti .

Tato metoda je operace O(n), kde n je Length .array

Viz také

Platí pro