Freigeben über


Array.IndexOf Methode

Definition

Sucht nach dem angegebenen Objekt und gibt den Index des ersten Vorkommens in einem eindimensionalen Array oder in einem Bereich von Elementen im Array zurück.

Überlädt

Name Beschreibung
IndexOf(Array, Object)

Sucht nach dem angegebenen Objekt und gibt den Index des ersten Vorkommens in einem eindimensionalen Array zurück.

IndexOf(Array, Object, Int32)

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index bis zum Ende des Arrays.

IndexOf(Array, Object, Int32, Int32)

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index für eine bestimmte Anzahl von Elementen.

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

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index bis zum Ende des Arrays.

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

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index für eine bestimmte Anzahl von Elementen.

IndexOf<T>(T[], T)

Sucht nach dem angegebenen Objekt und gibt den Index des ersten Vorkommens in einem eindimensionalen Array zurück.

IndexOf(Array, Object)

Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs

Sucht nach dem angegebenen Objekt und gibt den Index des ersten Vorkommens in einem eindimensionalen Array zurück.

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

Parameter

array
Array

Das eindimensionale Array, das durchsucht werden soll.

value
Object

Das Objekt, in arraydem gesucht werden soll.

Gibt zurück

Der Index des ersten Vorkommens von value in array, falls gefunden; andernfalls die untere Grenze des Arrays minus 1.

Ausnahmen

array ist null.

array ist multidimensional.

Beispiele

Im Beispiel werden die folgenden drei Überladungen der IndexOf Methode aufgerufen, um den Index einer Zeichenfolge in einem Zeichenfolgenarray zu finden:

  • IndexOf(Array, Object), um das erste Vorkommen der Zeichenfolge "the" in einem Zeichenfolgenarray zu bestimmen.

  • IndexOf(Array, Object, Int32), um das erste Vorkommen der Zeichenfolge "the" im vierten bis zu den letzten Elementen eines Zeichenfolgenarrays zu bestimmen.

  • IndexOf(Array, Object, Int32, Int32), um das erste Vorkommen der Zeichenfolge "the" in einem Zeichenfolgenarray aus dem Element zu bestimmen, das der letzten erfolgreichen Übereinstimmung am Ende des Arrays folgt.

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

Hinweise

Diese Methode durchsucht alle Elemente eines eindimensionalen Arrays nach value. Um festzustellen, ob value vorhanden ist array, führt die Methode einen Gleichheitsvergleich mithilfe des Standardmäßigen Gleichheitsvergleichs EqualityComparer<T>.Defaultdurch.

Da die meisten Arrays eine untere Grenze von Null aufweisen, gibt diese Methode in der Regel -1 zurück, wennvalue sie nicht gefunden wird. In dem seltenen Fall, dass die untere Grenze des Arrays gleich Int32.MinValue(0x80000000) ist und value nicht gefunden wird, gibt diese Methode (0x7FFFFFFF) zurück Int32.MaxValue .

Bei dieser Methode handelt es sich um einen O()-Vorgang, bei dem n es sich um einen Length Oarray(n)-Vorgang handelt.

Weitere Informationen

Gilt für:

IndexOf(Array, Object, Int32)

Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index bis zum Ende des Arrays.

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

Parameter

array
Array

Das eindimensionale Array, das durchsucht werden soll.

value
Object

Das Objekt, in arraydem gesucht werden soll.

startIndex
Int32

Der Startindex der Suche. 0 (Null) ist in einem leeren Array gültig.

Gibt zurück

Der Index des ersten Vorkommens von value, wenn es gefunden wird, innerhalb des Bereichs der Elemente, die array sich von startIndex zum letzten Element erstreckt; andernfalls die untere Grenze des Arrays minus 1.

Ausnahmen

array ist null.

startIndex liegt außerhalb des Bereichs gültiger Indizes für array.

array ist multidimensional.

Beispiele

Im Beispiel werden die folgenden drei Überladungen der IndexOf Methode aufgerufen, um den Index einer Zeichenfolge in einem Zeichenfolgenarray zu finden:

  • IndexOf(Array, Object), um das erste Vorkommen der Zeichenfolge "the" in einem Zeichenfolgenarray zu bestimmen.

  • IndexOf(Array, Object, Int32), um das erste Vorkommen der Zeichenfolge "the" im vierten bis zu den letzten Elementen eines Zeichenfolgenarrays zu bestimmen.

  • IndexOf(Array, Object, Int32, Int32), um das erste Vorkommen der Zeichenfolge "the" in einem Zeichenfolgenarray aus dem Element zu bestimmen, das der letzten erfolgreichen Übereinstimmung am Ende des Arrays folgt.

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

Hinweise

Diese Methode durchsucht ein eindimensionales Array aus dem Element am Index startIndex bis zum letzten Element. Um festzustellen, ob value vorhanden ist array, führt die Methode einen Gleichheitsvergleich mithilfe des Standardmäßigen Gleichheitsvergleichs EqualityComparer<T>.Defaultdurch.

Da die meisten Arrays eine untere Grenze von Null aufweisen, gibt diese Methode in der Regel -1 zurück, wenn value sie nicht gefunden wird. In dem seltenen Fall, dass die untere Grenze des Arrays gleich Int32.MinValue(0x80000000) ist und value nicht gefunden wird, gibt diese Methode (0x7FFFFFFF) zurück Int32.MaxValue .

Wenn startIndex gleich Array.Length,die Methode gibt -1 zurück. Wenn startIndex größer als Array.Length, löst die Methode eine ArgumentOutOfRangeException.

Diese Methode ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente von startIndex bis zum Ende von array.

Weitere Informationen

Gilt für:

IndexOf(Array, Object, Int32, Int32)

Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index für eine bestimmte Anzahl von Elementen.

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

Parameter

array
Array

Das eindimensionale Array, das durchsucht werden soll.

value
Object

Das Objekt, in arraydem gesucht werden soll.

startIndex
Int32

Der Startindex der Suche. 0 (Null) ist in einem leeren Array gültig.

count
Int32

Die Anzahl der zu durchsuchenden Elemente.

Gibt zurück

Der Index des ersten Vorkommens von value, wenn es im array Index startIndex zu startIndex + count - 1 gefunden wird; andernfalls die untere Grenze des Arrays minus 1.

Ausnahmen

array ist null.

startIndex liegt außerhalb des Bereichs gültiger Indizes für array.

- oder -

count ist kleiner als 0 (null).

- oder -

startIndex und count geben Sie keinen gültigen Abschnitt in array.

array ist multidimensional.

Beispiele

Im Beispiel werden die folgenden drei Überladungen der IndexOf Methode aufgerufen, um den Index einer Zeichenfolge in einem Zeichenfolgenarray zu finden:

  • IndexOf(Array, Object), um das erste Vorkommen der Zeichenfolge "the" in einem Zeichenfolgenarray zu bestimmen.

  • IndexOf(Array, Object, Int32), um das erste Vorkommen der Zeichenfolge "the" im vierten bis zu den letzten Elementen eines Zeichenfolgenarrays zu bestimmen.

  • IndexOf(Array, Object, Int32, Int32), um das erste Vorkommen der Zeichenfolge "the" in einem Zeichenfolgenarray aus dem Element zu bestimmen, das der letzten erfolgreichen Übereinstimmung am Ende des Arrays folgt. Um den Wert des count Arguments zu bestimmen, subtrahiert er die obere Grenze des Arrays vom Startindex und fügt einen hinzu.

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

Hinweise

Diese Methode durchsucht die Elemente eines eindimensionalen Arrays von startIndex plus countstartIndex minus 1, wenn count größer als 0 ist. Um festzustellen, ob value vorhanden ist array, führt die Methode einen Gleichheitsvergleich mithilfe des Standardmäßigen Gleichheitsvergleichs EqualityComparer<T>.Defaultdurch.

Da die meisten Arrays eine untere Grenze von Null aufweisen, gibt diese Methode in der Regel -1 zurück, wenn value sie nicht gefunden wird. In dem seltenen Fall, dass die untere Grenze des Arrays gleich Int32.MinValue (0x80000000) ist und value nicht gefunden wird, gibt diese Methode (0x7FFFFFFF) zurück Int32.MaxValue .

Ist startindex gleich Array.Length, gibt die Methode -1 zurück. Wenn startIndex größer als Array.Length, löst die Methode eine ArgumentOutOfRangeException.

Bei dieser Methode handelt es sich um einen O()-Vorgang, wobei n es sich um einen O(n)-Vorgang handeltcount.

Weitere Informationen

Gilt für:

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

Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index bis zum Ende des Arrays.

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

Typparameter

T

Der Typ der Elemente des Arrays.

Parameter

array
T[]

Das eindimensionale, nullbasierte Array, das durchsucht werden soll.

value
T

Das Objekt, in arraydem gesucht werden soll.

startIndex
Int32

Der nullbasierte Startindex der Suche. 0 (Null) ist in einem leeren Array gültig.

Gibt zurück

Der nullbasierte Index des ersten Vorkommens value innerhalb des Elementbereichs array , der sich von startIndex bis zum letzten Element erstreckt, falls gefunden; andernfalls -1.

Ausnahmen

array ist null.

startIndex liegt außerhalb des Bereichs gültiger Indizes für array.

Beispiele

Im folgenden Beispiel werden alle drei generischen Überladungen der IndexOf Methode veranschaulicht. Es wird ein Array mit Zeichenfolgen erstellt, wobei ein Eintrag zweimal angezeigt wird, an Indexspeicherort 0 und Indexspeicherort 5. Die IndexOf<T>(T[], T) Methodenüberladung durchsucht das Array von Anfang an und findet das erste Vorkommen der Zeichenfolge. Die IndexOf<T>(T[], T, Int32) Methodenüberladung wird verwendet, um das Array ab Indexposition 3 zu durchsuchen und das Ende des Arrays fortzusetzen und das zweite Vorkommen der Zeichenfolge zu finden. Schließlich wird die IndexOf<T>(T[], T, Int32, Int32) Methodenüberladung verwendet, um einen Bereich von zwei Einträgen zu durchsuchen, beginnend am Indexspeicherort 2. Sie gibt -1 zurück, da in diesem Bereich keine Instanzen der Suchzeichenfolge vorhanden sind.

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

Hinweise

Diese Methode durchsucht ein eindimensionales Array vom Element am startIndex Ende des Arrays. Um festzustellen, ob value vorhanden ist array, führt die Methode einen Gleichheitsvergleich mithilfe des Standardmäßigen Gleichheitsvergleichs EqualityComparer<T>.Defaultdurch.

Wenn startIndex gleich Length,die Methode gibt -1 zurück. Wenn startIndex größer als Array.Length, löst die Methode eine ArgumentOutOfRangeException.

Diese Methode ist ein O(n)-Vorgang, wobei n die Anzahl der Elemente von startIndex bis zum Ende von array.

Weitere Informationen

Gilt für:

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

Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs

Sucht nach dem angegebenen Objekt in einem Bereich von Elementen eines eindimensionalen Arrays und gibt den Index des ersten Vorkommens zurück. Der Bereich erstreckt sich von einem angegebenen Index für eine bestimmte Anzahl von Elementen.

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

Typparameter

T

Der Typ der Elemente des Arrays.

Parameter

array
T[]

Das eindimensionale, nullbasierte Array, das durchsucht werden soll.

value
T

Das Objekt, in arraydem gesucht werden soll.

startIndex
Int32

Der nullbasierte Startindex der Suche. 0 (Null) ist in einem leeren Array gültig.

count
Int32

Die Anzahl der zu durchsuchenden Elemente im Abschnitt.

Gibt zurück

Der nullbasierte Index des ersten Vorkommens value innerhalb des Elementbereichs, der array beginnt bei startIndex und enthält die Anzahl der elemente, die in count, falls gefunden, angegeben sind; andernfalls -1.

Ausnahmen

array ist null.

startIndex liegt außerhalb des Bereichs gültiger Indizes für array.

- oder -

count ist kleiner als 0 (null).

- oder -

startIndex und count geben Sie keinen gültigen Abschnitt in array.

Beispiele

Im folgenden Beispiel werden alle drei generischen Überladungen der IndexOf Methode veranschaulicht. Es wird ein Array mit Zeichenfolgen erstellt, wobei ein Eintrag zweimal angezeigt wird, an Indexspeicherort 0 und Indexspeicherort 5. Die IndexOf<T>(T[], T) Methodenüberladung durchsucht das Array von Anfang an und findet das erste Vorkommen der Zeichenfolge. Die IndexOf<T>(T[], T, Int32) Methodenüberladung wird verwendet, um das Array ab Indexposition 3 zu durchsuchen und das Ende des Arrays fortzusetzen und das zweite Vorkommen der Zeichenfolge zu finden. Schließlich wird die IndexOf<T>(T[], T, Int32, Int32) Methodenüberladung verwendet, um einen Bereich von zwei Einträgen zu durchsuchen, beginnend am Indexspeicherort 2. Sie gibt -1 zurück, da in diesem Bereich keine Instanzen der Suchzeichenfolge vorhanden sind.

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

Hinweise

Diese Methode durchsucht die Elemente eines eindimensionalen Arrays von startIndex plus countstartIndex minus 1, wenn count größer als 0 ist. Um festzustellen, ob value vorhanden ist array, führt die Methode einen Gleichheitsvergleich mithilfe des Standardmäßigen Gleichheitsvergleichs EqualityComparer<T>.Defaultdurch.

Ist startIndex gleich Array.Length, gibt die Methode -1 zurück. Wenn startIndex größer als Array.Length, löst die Methode eine ArgumentOutOfRangeException.

Bei dieser Methode handelt es sich um einen O()-Vorgang, wobei n es sich um einen O(n)-Vorgang handeltcount.

Weitere Informationen

Gilt für:

IndexOf<T>(T[], T)

Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs
Quelle:
Array.cs

Sucht nach dem angegebenen Objekt und gibt den Index des ersten Vorkommens in einem eindimensionalen Array zurück.

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

Typparameter

T

Der Typ der Elemente des Arrays.

Parameter

array
T[]

Das eindimensionale, nullbasierte Array, das durchsucht werden soll.

value
T

Das Objekt, in arraydem gesucht werden soll.

Gibt zurück

Der nullbasierte Index des ersten Vorkommens im gesamten array, value falls gefunden; andernfalls -1.

Ausnahmen

array ist null.

Beispiele

Im folgenden Beispiel werden alle drei generischen Überladungen der IndexOf Methode veranschaulicht. Es wird ein Array mit Zeichenfolgen erstellt, wobei ein Eintrag zweimal angezeigt wird, an Indexspeicherort 0 und Indexspeicherort 5. Die IndexOf<T>(T[], T) Methodenüberladung durchsucht das Array von Anfang an und findet das erste Vorkommen der Zeichenfolge. Die IndexOf<T>(T[], T, Int32) Methodenüberladung wird verwendet, um das Array ab Indexposition 3 zu durchsuchen und das Ende des Arrays fortzusetzen und das zweite Vorkommen der Zeichenfolge zu finden. Schließlich wird die IndexOf<T>(T[], T, Int32, Int32) Methodenüberladung verwendet, um einen Bereich von zwei Einträgen zu durchsuchen, beginnend am Indexspeicherort 2. Sie gibt -1 zurück, da in diesem Bereich keine Instanzen der Suchzeichenfolge vorhanden sind.

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

Hinweise

Diese Methode durchsucht alle Elemente eines eindimensionalen Arrays nach value. Um festzustellen, ob value vorhanden ist array, führt die Methode einen Gleichheitsvergleich mithilfe des Standardmäßigen Gleichheitsvergleichs EqualityComparer<T>.Defaultdurch.

Bei dieser Methode handelt es sich um einen O()-Vorgang, bei dem n es sich um einen Length Oarray(n)-Vorgang handelt.

Weitere Informationen

Gilt für: