Udostępnij za pośrednictwem


Array.IndexOf Metoda

Definicja

Wyszukuje określony obiekt i zwraca indeks pierwszego wystąpienia w tablicy jednowymiarowej lub w zakresie elementów w tablicy.

Przeciążenia

Nazwa Opis
IndexOf(Array, Object)

Wyszukuje określony obiekt i zwraca indeks pierwszego wystąpienia w tablicy jednowymiarowej.

IndexOf(Array, Object, Int32)

Wyszukuje określony obiekt w zakresie elementów jednowymiarowej tablicy i zwraca indeks pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu na koniec tablicy.

IndexOf(Array, Object, Int32, Int32)

Wyszukuje określony obiekt w zakresie elementów tablicy jednowymiarowej i zwraca indeks ifs pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu dla określonej liczby elementów.

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

Wyszukuje określony obiekt w zakresie elementów jednowymiarowej tablicy i zwraca indeks pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu na koniec tablicy.

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

Wyszukuje określony obiekt w zakresie elementów jednowymiarowej tablicy i zwraca indeks pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu dla określonej liczby elementów.

IndexOf<T>(T[], T)

Wyszukuje określony obiekt i zwraca indeks pierwszego wystąpienia w tablicy jednowymiarowej.

IndexOf(Array, Object)

Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs

Wyszukuje określony obiekt i zwraca indeks pierwszego wystąpienia w tablicy jednowymiarowej.

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

Tablica jednowymiarowa do wyszukiwania.

value
Object

Obiekt do zlokalizowania w pliku array.

Zwraca

Indeks pierwszego wystąpienia value elementu w arraymetodzie , jeśli zostanie znaleziony; w przeciwnym razie dolna granica tablicy minus 1.

Wyjątki

Parametr array ma wartość null.

array jest wielowymiarowa.

Przykłady

W przykładzie wywołane są następujące trzy przeciążenia metody w celu znalezienia indeksu ciągu w tablicy ciągów IndexOf :

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

Uwagi

Ta metoda wyszukuje wszystkie elementy tablicy jednowymiarowej dla elementu value. Aby określić, czy value istnieje w arraymetodzie , metoda wykonuje porównanie równości przy użyciu domyślnego porównania EqualityComparer<T>.Defaultrówności .

Ponieważ większość tablic ma dolną granicę zera, ta metoda zwykle zwraca -1, jeślivalue nie zostanie znaleziona. W rzadkim przypadku, gdy dolna granica tablicy jest równa Int32.MinValue(0x80000000) i value nie zostanie znaleziona, ta metoda zwraca Int32.MaxValue wartość (0x7FFFFFFF).

Ta metoda jest operacją O(), gdzie n jest operacją Lengtharray.n

Zobacz też

Dotyczy

IndexOf(Array, Object, Int32)

Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs

Wyszukuje określony obiekt w zakresie elementów jednowymiarowej tablicy i zwraca indeks pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu na koniec tablicy.

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

Tablica jednowymiarowa do wyszukiwania.

value
Object

Obiekt do zlokalizowania w pliku array.

startIndex
Int32

Indeks początkowy wyszukiwania. Wartość 0 (zero) jest prawidłowa w pustej tablicy.

Zwraca

Indeks pierwszego wystąpienia valueklasy , jeśli zostanie znaleziony, w zakresie elementów w array tym rozciąga się od startIndex do ostatniego elementu; w przeciwnym razie dolna granica tablicy minus 1.

Wyjątki

Parametr array ma wartość null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.

array jest wielowymiarowa.

Przykłady

W przykładzie wywołane są następujące trzy przeciążenia metody w celu znalezienia indeksu ciągu w tablicy ciągów IndexOf :

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

Uwagi

Ta metoda wyszukuje tablicę jednowymiarową z elementu w indeksie startIndex do ostatniego elementu. Aby określić, czy value istnieje w arraymetodzie , metoda wykonuje porównanie równości przy użyciu domyślnego porównania EqualityComparer<T>.Defaultrówności .

Ponieważ większość tablic ma dolną granicę zera, ta metoda zazwyczaj zwraca -1, jeśli value nie zostanie znaleziona. W rzadkim przypadku, gdy dolna granica tablicy jest równa Int32.MinValue(0x80000000) i value nie zostanie znaleziona, ta metoda zwraca Int32.MaxValue wartość (0x7FFFFFFF).

Jeśli startIndex równa Array.Lengthsię , metoda zwraca -1. Jeśli startIndex wartość jest większa niż Array.Length, metoda zgłasza błąd ArgumentOutOfRangeException.

Ta metoda jest operacją O(n), gdzie n jest liczbą elementów z startIndex do końca array.

Zobacz też

Dotyczy

IndexOf(Array, Object, Int32, Int32)

Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs

Wyszukuje określony obiekt w zakresie elementów tablicy jednowymiarowej i zwraca indeks ifs pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu dla określonej liczby elementów.

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

Tablica jednowymiarowa do wyszukiwania.

value
Object

Obiekt do zlokalizowania w pliku array.

startIndex
Int32

Indeks początkowy wyszukiwania. Wartość 0 (zero) jest prawidłowa w pustej tablicy.

count
Int32

Liczba elementów do wyszukania.

Zwraca

Indeks pierwszego wystąpienia valueklasy , jeśli znajduje się w indeksie startIndexarray od do startIndex + count -1; w przeciwnym razie dolna granica tablicy minus 1.

Wyjątki

Parametr array ma wartość null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.

— lub —

Parametr count ma wartość niższą niż zero.

— lub —

startIndex i count nie należy określać prawidłowej sekcji w pliku array.

array jest wielowymiarowa.

Przykłady

W przykładzie wywołane są następujące trzy przeciążenia metody w celu znalezienia indeksu ciągu w tablicy ciągów IndexOf :

  • IndexOf(Array, Object), aby określić pierwsze wystąpienie ciągu "the" w tablicy ciągów.

  • IndexOf(Array, Object, Int32), aby określić pierwsze wystąpienie ciągu "the" w czwartym do ostatnich elementów tablicy ciągów.

  • IndexOf(Array, Object, Int32, Int32), aby określić pierwsze wystąpienie ciągu "the" w tablicy ciągów z elementu, który następuje po ostatnim pomyślnym dopasowaniu na końcu tablicy. Aby określić wartość argumentu count , odejmuje górną granicę tablicy z indeksu początkowego i dodaje jeden.

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

Uwagi

Ta metoda wyszukuje elementy tablicy jednowymiarowej z startIndex do startIndex plus count minus 1, jeśli count jest większa niż 0. Aby określić, czy value istnieje w arraymetodzie , metoda wykonuje porównanie równości przy użyciu domyślnego porównania EqualityComparer<T>.Defaultrówności .

Ponieważ większość tablic ma niższą granicę zera, ta metoda zazwyczaj zwraca -1, gdy value nie zostanie znaleziona. W rzadkich przypadkach, gdy dolna granica tablicy jest równa Int32.MinValue (0x80000000) i value nie zostanie znaleziona, ta metoda zwraca Int32.MaxValue wartość (0x7FFFFFFF).

Jeśli startindex równa Array.Lengthsię , metoda zwraca wartość -1. Jeśli startIndex wartość jest większa niż Array.Length, metoda zgłasza błąd ArgumentOutOfRangeException.

Ta metoda jest operacją O(n), gdzie n to count.

Zobacz też

Dotyczy

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

Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs

Wyszukuje określony obiekt w zakresie elementów jednowymiarowej tablicy i zwraca indeks pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu na koniec tablicy.

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 elementów tablicy.

Parametry

array
T[]

Jednowymiarowa, zero-oparta tablica do wyszukania.

value
T

Obiekt do zlokalizowania w pliku array.

startIndex
Int32

Zerowy indeks początkowy wyszukiwania. Wartość 0 (zero) jest prawidłowa w pustej tablicy.

Zwraca

Indeks oparty na zera pierwszego wystąpienia value w zakresie elementów w array tym rozciąga się od startIndex do ostatniego elementu, jeśli zostanie znaleziony; w przeciwnym razie - 1.

Wyjątki

Parametr array ma wartość null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.

Przykłady

W poniższym przykładzie przedstawiono wszystkie trzy ogólne przeciążenia IndexOf metody. Zostanie utworzona tablica ciągów z jednym wpisem, który pojawia się dwa razy, w lokalizacji indeksu 0 i lokalizacji indeksu 5. Metoda IndexOf<T>(T[], T) przeszukuje tablicę od początku i znajduje pierwsze wystąpienie ciągu. Przeciążenie IndexOf<T>(T[], T, Int32) metody służy do przeszukiwania tablicy rozpoczynającej się od lokalizacji indeksu 3 i kontynuowania na końcu tablicy i znajduje drugie wystąpienie ciągu. IndexOf<T>(T[], T, Int32, Int32) Na koniec przeciążenie metody służy do wyszukiwania zakresu dwóch wpisów, zaczynając od lokalizacji indeksu drugiego. Zwraca -1, ponieważ w tym zakresie nie ma wystąpień ciągu wyszukiwania.

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

Uwagi

Ta metoda wyszukuje tablicę jednowymiarową z elementu na startIndex końcu tablicy. Aby określić, czy value istnieje w arraymetodzie , metoda wykonuje porównanie równości przy użyciu domyślnego porównania EqualityComparer<T>.Defaultrówności .

Jeśli startIndex równa Lengthsię , metoda zwraca -1. Jeśli startIndex wartość jest większa niż Array.Length, metoda zgłasza błąd ArgumentOutOfRangeException.

Ta metoda jest operacją O(n), gdzie n jest liczbą elementów z startIndex do końca array.

Zobacz też

Dotyczy

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

Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs

Wyszukuje określony obiekt w zakresie elementów jednowymiarowej tablicy i zwraca indeks pierwszego wystąpienia. Zakres rozciąga się od określonego indeksu dla określonej liczby elementów.

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 elementów tablicy.

Parametry

array
T[]

Jednowymiarowa, zero-oparta tablica do wyszukania.

value
T

Obiekt do zlokalizowania w pliku array.

startIndex
Int32

Zerowy indeks początkowy wyszukiwania. Wartość 0 (zero) jest prawidłowa w pustej tablicy.

count
Int32

Liczba elementów w sekcji do wyszukania.

Zwraca

Indeks na podstawie zera pierwszego wystąpienia value w zakresie elementów, array które zaczynają się od startIndex i zawiera liczbę elementów określonych w count, jeśli zostanie znaleziona; w przeciwnym razie -1.

Wyjątki

Parametr array ma wartość null.

startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.

— lub —

Parametr count ma wartość niższą niż zero.

— lub —

startIndex i count nie należy określać prawidłowej sekcji w pliku array.

Przykłady

W poniższym przykładzie przedstawiono wszystkie trzy ogólne przeciążenia IndexOf metody. Zostanie utworzona tablica ciągów z jednym wpisem, który pojawia się dwa razy, w lokalizacji indeksu 0 i lokalizacji indeksu 5. Metoda IndexOf<T>(T[], T) przeszukuje tablicę od początku i znajduje pierwsze wystąpienie ciągu. Przeciążenie IndexOf<T>(T[], T, Int32) metody służy do przeszukiwania tablicy rozpoczynającej się od lokalizacji indeksu 3 i kontynuowania na końcu tablicy i znajduje drugie wystąpienie ciągu. IndexOf<T>(T[], T, Int32, Int32) Na koniec przeciążenie metody służy do wyszukiwania zakresu dwóch wpisów, zaczynając od lokalizacji indeksu drugiego. Zwraca -1, ponieważ w tym zakresie nie ma wystąpień ciągu wyszukiwania.

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

Uwagi

Ta metoda wyszukuje elementy tablicy jednowymiarowej z startIndex do startIndex plus count minus 1, jeśli count jest większa niż 0. Aby określić, czy value istnieje w arraymetodzie , metoda wykonuje porównanie równości przy użyciu domyślnego porównania EqualityComparer<T>.Defaultrówności .

Jeśli startIndex równa Array.Lengthsię , metoda zwraca wartość -1. Jeśli startIndex wartość jest większa niż Array.Length, metoda zgłasza błąd ArgumentOutOfRangeException.

Ta metoda jest operacją O(n), gdzie n to count.

Zobacz też

Dotyczy

IndexOf<T>(T[], T)

Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs
Źródło:
Array.cs

Wyszukuje określony obiekt i zwraca indeks pierwszego wystąpienia w tablicy jednowymiarowej.

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 elementów tablicy.

Parametry

array
T[]

Jednowymiarowa, zero-oparta tablica do wyszukania.

value
T

Obiekt do zlokalizowania w pliku array.

Zwraca

Indeks oparty na zera pierwszego wystąpienia value w całym arrayobiekcie , jeśli zostanie znaleziony; w przeciwnym razie -1.

Wyjątki

Parametr array ma wartość null.

Przykłady

W poniższym przykładzie przedstawiono wszystkie trzy ogólne przeciążenia IndexOf metody. Zostanie utworzona tablica ciągów z jednym wpisem, który pojawia się dwa razy, w lokalizacji indeksu 0 i lokalizacji indeksu 5. Metoda IndexOf<T>(T[], T) przeszukuje tablicę od początku i znajduje pierwsze wystąpienie ciągu. Przeciążenie IndexOf<T>(T[], T, Int32) metody służy do przeszukiwania tablicy rozpoczynającej się od lokalizacji indeksu 3 i kontynuowania na końcu tablicy i znajduje drugie wystąpienie ciągu. IndexOf<T>(T[], T, Int32, Int32) Na koniec przeciążenie metody służy do wyszukiwania zakresu dwóch wpisów, zaczynając od lokalizacji indeksu drugiego. Zwraca -1, ponieważ w tym zakresie nie ma wystąpień ciągu wyszukiwania.

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

Uwagi

Ta metoda wyszukuje wszystkie elementy tablicy jednowymiarowej dla elementu value. Aby określić, czy value istnieje w arraymetodzie , metoda wykonuje porównanie równości przy użyciu domyślnego porównania EqualityComparer<T>.Defaultrówności .

Ta metoda jest operacją O(), gdzie n jest operacją Lengtharray.n

Zobacz też

Dotyczy