Array.IndexOf Methode
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Zoekt naar het opgegeven object en retourneert de index van het eerste exemplaar in een eendimensionale matrix of in een bereik met elementen in de matrix.
Overloads
| Name | Description |
|---|---|
| IndexOf(Array, Object) |
Zoekt naar het opgegeven object en retourneert de index van het eerste exemplaar in een eendimensionale matrix. |
| IndexOf(Array, Object, Int32) |
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van het eerste exemplaar. Het bereik is een uitbreiding van een opgegeven index tot het einde van de matrix. |
| IndexOf(Array, Object, Int32, Int32) |
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van ifs voor het eerst. Het bereik is een uitbreiding van een opgegeven index voor een opgegeven aantal elementen. |
| IndexOf<T>(T[], T, Int32) |
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van het eerste exemplaar. Het bereik is een uitbreiding van een opgegeven index tot het einde van de matrix. |
| IndexOf<T>(T[], T, Int32, Int32) |
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van het eerste exemplaar. Het bereik is een uitbreiding van een opgegeven index voor een opgegeven aantal elementen. |
| IndexOf<T>(T[], T) |
Zoekt naar het opgegeven object en retourneert de index van het eerste exemplaar in een eendimensionale matrix. |
IndexOf(Array, Object)
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
Zoekt naar het opgegeven object en retourneert de index van het eerste exemplaar in een eendimensionale matrix.
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
Parameters
- array
- Array
De eendimensionale matrix die moet worden gezocht.
- value
- Object
Het object dat moet worden gevonden in array.
Retouren
De index van het eerste exemplaar van value in array, indien gevonden; anders de ondergrens van de matrix min 1.
Uitzonderingen
array is null.
array is multidimensionaal.
Voorbeelden
In het voorbeeld worden de volgende drie overbelastingen van de IndexOf methode aangeroepen om de index van een tekenreeks in een tekenreeksmatrix te vinden:
IndexOf(Array, Object), om het eerste exemplaar van de tekenreeks 'the' in een tekenreeksmatrix te bepalen.
IndexOf(Array, Object, Int32), om het eerste exemplaar van de tekenreeks 'de' in de vierde tot de laatste elementen van een tekenreeksmatrix te bepalen.
IndexOf(Array, Object, Int32, Int32), om het eerste exemplaar van de tekenreeks 'the' in een tekenreeksmatrix te bepalen van het element dat volgt op de laatste geslaagde overeenkomst aan het einde van de matrix.
// 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.
Opmerkingen
Met deze methode wordt gezocht naar alle elementen van een eendimensionale matrix voor value. Om te bepalen of value er in bestaat array, voert de methode een gelijkheidsvergelijking uit met behulp van de standaard gelijkheidsvergelijker EqualityComparer<T>.Default.
Omdat de meeste matrices een ondergrens van nul hebben, retourneert deze methode over het algemeen -1 alsvalue deze niet wordt gevonden. In het zeldzame geval dat de ondergrens van de matrix gelijk is aan Int32.MinValue(0x80000000) en value niet wordt gevonden, retourneert Int32.MaxValue deze methode (0x7FFFFFFF).
Deze methode is een O(n)-bewerking, waarbij n de Length van array.
Zie ook
Van toepassing op
IndexOf(Array, Object, Int32)
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van het eerste exemplaar. Het bereik is een uitbreiding van een opgegeven index tot het einde van de matrix.
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
Parameters
- array
- Array
De eendimensionale matrix die moet worden gezocht.
- value
- Object
Het object dat moet worden gevonden in array.
- startIndex
- Int32
De beginindex van de zoekopdracht. 0 (nul) is geldig in een lege matrix.
Retouren
De index van het eerste exemplaar van value, als deze wordt gevonden, binnen het bereik van elementen in array die zich uitbreidt van startIndex het laatste element, anders de ondergrens van de matrix min 1.
Uitzonderingen
array is null.
startIndex valt buiten het bereik van geldige indexen voor array.
array is multidimensionaal.
Voorbeelden
In het voorbeeld worden de volgende drie overbelastingen van de IndexOf methode aangeroepen om de index van een tekenreeks in een tekenreeksmatrix te vinden:
IndexOf(Array, Object), om het eerste exemplaar van de tekenreeks 'the' in een tekenreeksmatrix te bepalen.
IndexOf(Array, Object, Int32), om het eerste exemplaar van de tekenreeks 'de' in de vierde tot de laatste elementen van een tekenreeksmatrix te bepalen.
IndexOf(Array, Object, Int32, Int32), om het eerste exemplaar van de tekenreeks 'the' in een tekenreeksmatrix te bepalen van het element dat volgt op de laatste geslaagde overeenkomst aan het einde van de matrix.
// 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.
Opmerkingen
Met deze methode wordt in een eendimensionale matrix van het element in de index startIndex naar het laatste element gezocht. Om te bepalen of value er in bestaat array, voert de methode een gelijkheidsvergelijking uit met behulp van de standaard gelijkheidsvergelijker EqualityComparer<T>.Default.
Omdat de meeste matrices een ondergrens van nul hebben, retourneert deze methode over het algemeen -1 als value deze niet wordt gevonden. In het zeldzame geval dat de ondergrens van de matrix gelijk is aan Int32.MinValue(0x80000000) en value niet wordt gevonden, retourneert Int32.MaxValue deze methode (0x7FFFFFFF).
Als startIndex deze gelijk is Array.Lengthaan, retourneert de methode -1. Als startIndex de methode groter is dan Array.Length, werpt de methode een ArgumentOutOfRangeException.
Deze methode is een O(n)-bewerking, waarbij n het aantal elementen van startIndex tot het einde van array.
Zie ook
Van toepassing op
IndexOf(Array, Object, Int32, Int32)
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van ifs voor het eerst. Het bereik is een uitbreiding van een opgegeven index voor een opgegeven aantal 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
Parameters
- array
- Array
De eendimensionale matrix die moet worden gezocht.
- value
- Object
Het object dat moet worden gevonden in array.
- startIndex
- Int32
De beginindex van de zoekopdracht. 0 (nul) is geldig in een lege matrix.
- count
- Int32
Het aantal elementen dat moet worden gezocht.
Retouren
De index van het eerste exemplaar van value, als deze wordt gevonden in de array index startIndex van startIndex + count - 1; anders de ondergrens van de matrix min 1.
Uitzonderingen
array is null.
startIndex valt buiten het bereik van geldige indexen voor array.
– of –
count is kleiner dan nul.
– of –
startIndex en count geef geen geldige sectie op in array.
array is multidimensionaal.
Voorbeelden
In het voorbeeld worden de volgende drie overbelastingen van de IndexOf methode aangeroepen om de index van een tekenreeks in een tekenreeksmatrix te vinden:
IndexOf(Array, Object), om het eerste exemplaar van de tekenreeks 'the' in een tekenreeksmatrix te bepalen.
IndexOf(Array, Object, Int32), om het eerste exemplaar van de tekenreeks 'de' in de vierde tot de laatste elementen van een tekenreeksmatrix te bepalen.
IndexOf(Array, Object, Int32, Int32), om het eerste exemplaar van de tekenreeks 'the' in een tekenreeksmatrix te bepalen van het element dat volgt op de laatste geslaagde overeenkomst aan het einde van de matrix. Als u de waarde van het
countargument wilt bepalen, wordt de bovengrens van de matrix afgetrokken van de beginindex en wordt er een toegevoegd.
// 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.
Opmerkingen
Met deze methode wordt gezocht naar de elementen van een eendimensionale matrix van startIndex plus startIndexcount min 1, als count deze groter is dan 0. Om te bepalen of value er in bestaat array, voert de methode een gelijkheidsvergelijking uit met behulp van de standaard gelijkheidsvergelijker EqualityComparer<T>.Default.
Omdat de meeste matrices een ondergrens van nul hebben, retourneert deze methode meestal -1 wanneer value deze niet wordt gevonden. In het zeldzame geval dat de ondergrens van de matrix gelijk is aan Int32.MinValue (0x80000000) en value niet wordt gevonden, retourneert Int32.MaxValue deze methode (0x7FFFFFFF).
Als startindex dit gelijk is Array.Lengthaan, retourneert de methode -1. Als startIndex de methode groter is dan Array.Length, werpt de methode een ArgumentOutOfRangeException.
Deze methode is een O(n)-bewerking, waarbij n .count
Zie ook
Van toepassing op
IndexOf<T>(T[], T, Int32)
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van het eerste exemplaar. Het bereik is een uitbreiding van een opgegeven index tot het einde van de matrix.
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
Type parameters
- T
Het type van de elementen van de matrix.
Parameters
- array
- T[]
De eendimensionale matrix op basis van nul om te zoeken.
- value
- T
Het object dat moet worden gevonden in array.
- startIndex
- Int32
De op nul gebaseerde startindex van de zoekopdracht. 0 (nul) is geldig in een lege matrix.
Retouren
De op nul gebaseerde index van het eerste exemplaar van value binnen het bereik van elementen in array die zich uitbreidt van startIndex het laatste element, indien gevonden; anders -1.
Uitzonderingen
array is null.
startIndex valt buiten het bereik van geldige indexen voor array.
Voorbeelden
In het volgende voorbeeld ziet u alle drie de algemene overbelastingen van de IndexOf methode. Er wordt een matrix met tekenreeksen gemaakt, met één vermelding die tweemaal wordt weergegeven, op indexlocatie 0 en indexlocatie 5. De IndexOf<T>(T[], T) methode overbelasting doorzoekt de matrix vanaf het begin en vindt het eerste exemplaar van de tekenreeks. De IndexOf<T>(T[], T, Int32) overbelasting van de methode wordt gebruikt om de matrix te doorzoeken vanaf indexlocatie 3 en door te gaan naar het einde van de matrix en het tweede exemplaar van de tekenreeks te vinden. Ten slotte wordt de overbelasting van de IndexOf<T>(T[], T, Int32, Int32) methode gebruikt om een bereik van twee vermeldingen te doorzoeken, te beginnen bij de indexlocatie twee; het retourneert -1 omdat er geen exemplaren van de zoekreeks in dat bereik zijn.
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
Opmerkingen
Met deze methode wordt een eendimensionale matrix van het element aan startIndex het einde van de matrix doorzocht. Om te bepalen of value er in bestaat array, voert de methode een gelijkheidsvergelijking uit met behulp van de standaard gelijkheidsvergelijker EqualityComparer<T>.Default.
Als startIndex deze gelijk is Lengthaan, retourneert de methode -1. Als startIndex de methode groter is dan Array.Length, werpt de methode een ArgumentOutOfRangeException.
Deze methode is een O(n)-bewerking, waarbij n het aantal elementen van startIndex tot het einde van array.
Zie ook
Van toepassing op
IndexOf<T>(T[], T, Int32, Int32)
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
Zoekt naar het opgegeven object in een reeks elementen van een eendimensionale matrix en retourneert de index van het eerste exemplaar. Het bereik is een uitbreiding van een opgegeven index voor een opgegeven aantal 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
Type parameters
- T
Het type van de elementen van de matrix.
Parameters
- array
- T[]
De eendimensionale matrix op basis van nul om te zoeken.
- value
- T
Het object dat moet worden gevonden in array.
- startIndex
- Int32
De op nul gebaseerde startindex van de zoekopdracht. 0 (nul) is geldig in een lege matrix.
- count
- Int32
Het aantal elementen in de sectie dat moet worden gezocht.
Retouren
De op nul gebaseerde index van het eerste exemplaar van value binnen het bereik van elementen in array die begint bij startIndex en bevat het aantal elementen dat is opgegeven in count, indien gevonden; anders -1.
Uitzonderingen
array is null.
startIndex valt buiten het bereik van geldige indexen voor array.
– of –
count is kleiner dan nul.
– of –
startIndex en count geef geen geldige sectie op in array.
Voorbeelden
In het volgende voorbeeld ziet u alle drie de algemene overbelastingen van de IndexOf methode. Er wordt een matrix met tekenreeksen gemaakt, met één vermelding die tweemaal wordt weergegeven, op indexlocatie 0 en indexlocatie 5. De IndexOf<T>(T[], T) methode overbelasting doorzoekt de matrix vanaf het begin en vindt het eerste exemplaar van de tekenreeks. De IndexOf<T>(T[], T, Int32) overbelasting van de methode wordt gebruikt om de matrix te doorzoeken vanaf indexlocatie 3 en door te gaan naar het einde van de matrix en het tweede exemplaar van de tekenreeks te vinden. Ten slotte wordt de overbelasting van de IndexOf<T>(T[], T, Int32, Int32) methode gebruikt om een bereik van twee vermeldingen te doorzoeken, te beginnen bij de indexlocatie twee; het retourneert -1 omdat er geen exemplaren van de zoekreeks in dat bereik zijn.
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
Opmerkingen
Met deze methode wordt gezocht naar de elementen van een eendimensionale matrix van startIndex plus startIndexcount min 1, als count deze groter is dan 0. Om te bepalen of value er in bestaat array, voert de methode een gelijkheidsvergelijking uit met behulp van de standaard gelijkheidsvergelijker EqualityComparer<T>.Default.
Als startIndex dit gelijk is Array.Lengthaan, retourneert de methode -1. Als startIndex de methode groter is dan Array.Length, werpt de methode een ArgumentOutOfRangeException.
Deze methode is een O(n)-bewerking, waarbij n .count
Zie ook
Van toepassing op
IndexOf<T>(T[], T)
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
- Bron:
- Array.cs
Zoekt naar het opgegeven object en retourneert de index van het eerste exemplaar in een eendimensionale matrix.
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
Type parameters
- T
Het type van de elementen van de matrix.
Parameters
- array
- T[]
De eendimensionale matrix op basis van nul om te zoeken.
- value
- T
Het object dat moet worden gevonden in array.
Retouren
De op nul gebaseerde index van het eerste exemplaar in value het hele arrayexemplaar, indien gevonden; anders -1.
Uitzonderingen
array is null.
Voorbeelden
In het volgende voorbeeld ziet u alle drie de algemene overbelastingen van de IndexOf methode. Er wordt een matrix met tekenreeksen gemaakt, met één vermelding die tweemaal wordt weergegeven, op indexlocatie 0 en indexlocatie 5. De IndexOf<T>(T[], T) methode overbelasting doorzoekt de matrix vanaf het begin en vindt het eerste exemplaar van de tekenreeks. De IndexOf<T>(T[], T, Int32) overbelasting van de methode wordt gebruikt om de matrix te doorzoeken vanaf indexlocatie 3 en door te gaan naar het einde van de matrix en het tweede exemplaar van de tekenreeks te vinden. Ten slotte wordt de overbelasting van de IndexOf<T>(T[], T, Int32, Int32) methode gebruikt om een bereik van twee vermeldingen te doorzoeken, te beginnen bij de indexlocatie twee; het retourneert -1 omdat er geen exemplaren van de zoekreeks in dat bereik zijn.
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
Opmerkingen
Met deze methode wordt gezocht naar alle elementen van een eendimensionale matrix voor value. Om te bepalen of value er in bestaat array, voert de methode een gelijkheidsvergelijking uit met behulp van de standaard gelijkheidsvergelijker EqualityComparer<T>.Default.
Deze methode is een O(n)-bewerking, waarbij n de Length van array.