Array.IndexOf Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Söker efter det angivna objektet och returnerar indexet för dess första förekomst i en endimensionell matris eller i ett område med element i matrisen.
Överlagringar
| Name | Description |
|---|---|
| IndexOf(Array, Object) |
Söker efter det angivna objektet och returnerar indexet för dess första förekomst i en endimensionell matris. |
| IndexOf(Array, Object, Int32) |
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för den första förekomsten. Intervallet sträcker sig från ett angivet index till slutet av matrisen. |
| IndexOf(Array, Object, Int32, Int32) |
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för ifs första förekomst. Intervallet sträcker sig från ett angivet index för ett angivet antal element. |
| IndexOf<T>(T[], T, Int32) |
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för dess första förekomst. Intervallet sträcker sig från ett angivet index till slutet av matrisen. |
| IndexOf<T>(T[], T, Int32, Int32) |
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för den första förekomsten. Intervallet sträcker sig från ett angivet index för ett angivet antal element. |
| IndexOf<T>(T[], T) |
Söker efter det angivna objektet och returnerar indexet för dess första förekomst i en endimensionell matris. |
IndexOf(Array, Object)
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
Söker efter det angivna objektet och returnerar indexet för dess första förekomst i en endimensionell matris.
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
Parametrar
- array
- Array
Den endimensionella matris som ska sökas.
- value
- Object
Objektet som ska hittas i array.
Returer
Indexet för den första förekomsten av value i array, om det hittas, annars den nedre gränsen för matrisen minus 1.
Undantag
array är null.
array är flerdimensionellt.
Exempel
Exemplet anropar följande tre överlagringar av IndexOf metoden för att hitta indexet för en sträng i en strängmatris:
IndexOf(Array, Object), för att fastställa den första förekomsten av strängen "the" i en strängmatris.
IndexOf(Array, Object, Int32), för att fastställa den första förekomsten av strängen "the" i det fjärde till sista elementen i en strängmatris.
IndexOf(Array, Object, Int32, Int32), för att fastställa den första förekomsten av strängen "the" i en strängmatris från elementet som följer den senaste lyckade matchningen till slutet av matrisen.
// 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.
Kommentarer
Den här metoden söker igenom alla element i en endimensionell matris efter value. För att avgöra om value finns i arrayutför metoden en likhetsjämförelse med hjälp av standardjämförelsejämförelsen EqualityComparer<T>.Default.
Eftersom de flesta matriser har en lägre gräns på noll returnerar den här metoden vanligtvis -1 omvalue den inte hittas. I det sällsynta fallet att matrisens nedre gräns är lika Int32.MinValuemed (0x80000000) och value inte hittas returnerar Int32.MaxValue den här metoden (0x7FFFFFFF).
Den här metoden är en O()-nåtgärd, där n är för Lengtharray.
Se även
Gäller för
IndexOf(Array, Object, Int32)
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för den första förekomsten. Intervallet sträcker sig från ett angivet index till slutet av matrisen.
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
Parametrar
- array
- Array
Den endimensionella matris som ska sökas.
- value
- Object
Objektet som ska hittas i array.
- startIndex
- Int32
Startindexet för sökningen. 0 (noll) är giltigt i en tom matris.
Returer
Indexet för den första förekomsten av value, om det hittas, inom området för element i array som sträcker sig från startIndex till det sista elementet, annars den nedre gränsen för matrisen minus 1.
Undantag
array är null.
startIndex ligger utanför intervallet för giltiga index för array.
array är flerdimensionellt.
Exempel
Exemplet anropar följande tre överlagringar av IndexOf metoden för att hitta indexet för en sträng i en strängmatris:
IndexOf(Array, Object), för att fastställa den första förekomsten av strängen "the" i en strängmatris.
IndexOf(Array, Object, Int32), för att fastställa den första förekomsten av strängen "the" i det fjärde till sista elementen i en strängmatris.
IndexOf(Array, Object, Int32, Int32), för att fastställa den första förekomsten av strängen "the" i en strängmatris från elementet som följer den senaste lyckade matchningen till slutet av matrisen.
// 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.
Kommentarer
Den här metoden söker i en endimensionell matris från elementet vid index startIndex till det sista elementet. För att avgöra om value finns i arrayutför metoden en likhetsjämförelse med hjälp av standardjämförelsejämförelsen EqualityComparer<T>.Default.
Eftersom de flesta matriser har en lägre gräns på noll returnerar den här metoden vanligtvis -1 om value den inte hittas. I det sällsynta fallet att matrisens nedre gräns är lika Int32.MinValuemed (0x80000000) och value inte hittas returnerar Int32.MaxValue den här metoden (0x7FFFFFFF).
Om startIndex är lika med returnerar Array.Lengthmetoden -1. Om startIndex är större än Array.Lengthgenererar metoden en ArgumentOutOfRangeException.
Den här metoden är en O(n)-åtgärd, där n är antalet element från startIndex till slutet av array.
Se även
Gäller för
IndexOf(Array, Object, Int32, Int32)
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för ifs första förekomst. Intervallet sträcker sig från ett angivet index för ett angivet antal element.
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
Parametrar
- array
- Array
Den endimensionella matris som ska sökas.
- value
- Object
Objektet som ska hittas i array.
- startIndex
- Int32
Startindexet för sökningen. 0 (noll) är giltigt i en tom matris.
- count
- Int32
Antalet element som ska sökas.
Returer
Indexet för den första förekomsten av value, om det finns i array från indexet startIndex till startIndex + count - 1, annars den nedre gränsen för matrisen minus 1.
Undantag
array är null.
startIndex ligger utanför intervallet för giltiga index för array.
-eller-
count är mindre än noll.
-eller-
startIndex och count ange inte ett giltigt avsnitt i array.
array är flerdimensionellt.
Exempel
Exemplet anropar följande tre överlagringar av IndexOf metoden för att hitta indexet för en sträng i en strängmatris:
IndexOf(Array, Object), för att fastställa den första förekomsten av strängen "the" i en strängmatris.
IndexOf(Array, Object, Int32), för att fastställa den första förekomsten av strängen "the" i det fjärde till sista elementen i en strängmatris.
IndexOf(Array, Object, Int32, Int32), för att fastställa den första förekomsten av strängen "the" i en strängmatris från elementet som följer den senaste lyckade matchningen till slutet av matrisen. För att fastställa värdet för
countargumentet subtraherar det matrisens övre gräns från startindexet och lägger till ett.
// 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.
Kommentarer
Den här metoden söker igenom elementen i en endimensionell matris från startIndex till startIndex plus count minus 1, om count är större än 0. För att avgöra om value finns i arrayutför metoden en likhetsjämförelse med hjälp av standardjämförelsejämförelsen EqualityComparer<T>.Default.
Eftersom de flesta matriser har en lägre gräns på noll returnerar den här metoden vanligtvis -1 när value den inte hittas. I det sällsynta fallet att matrisens nedre gräns är lika Int32.MinValue med (0x80000000) och value inte hittas returnerar Int32.MaxValue den här metoden (0x7FFFFFFF).
Om startindex är lika med returnerar Array.Lengthmetoden -1. Om startIndex är större än Array.Lengthgenererar metoden en ArgumentOutOfRangeException.
Den här metoden är en O()-nåtgärd, där n är count.
Se även
Gäller för
IndexOf<T>(T[], T, Int32)
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för dess första förekomst. Intervallet sträcker sig från ett angivet index till slutet av matrisen.
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
Typparametrar
- T
Typ av element i matrisen.
Parametrar
- array
- T[]
Den endimensionella, nollbaserade matrisen som ska sökas.
- value
- T
Objektet som ska hittas i array.
- startIndex
- Int32
Det nollbaserade startindexet för sökningen. 0 (noll) är giltigt i en tom matris.
Returer
Det nollbaserade indexet för den första förekomsten av value inom elementintervallet i array som sträcker sig från startIndex till det sista elementet, om det hittas, annars -1.
Undantag
array är null.
startIndex ligger utanför intervallet för giltiga index för array.
Exempel
I följande exempel visas alla tre allmänna överlagringar av IndexOf metoden. En matris med strängar skapas, med en post som visas två gånger, på indexplats 0 och indexplats 5. Metodens IndexOf<T>(T[], T) överlagring söker i matrisen från början och hittar den första förekomsten av strängen. Metodöverlagringen IndexOf<T>(T[], T, Int32) används för att söka i matrisen som börjar med indexplats 3 och fortsätta till slutet av matrisen och hittar den andra förekomsten av strängen. Slutligen används metodöverlagringen IndexOf<T>(T[], T, Int32, Int32) för att söka i ett intervall med två poster, som börjar på indexplats två. Den returnerar -1 eftersom det inte finns några instanser av söksträngen i det intervallet.
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
Kommentarer
Den här metoden söker i en endimensionell matris från elementet i startIndex slutet av matrisen. För att avgöra om value finns i arrayutför metoden en likhetsjämförelse med hjälp av standardjämförelsejämförelsen EqualityComparer<T>.Default.
Om startIndex är lika med returnerar Lengthmetoden -1. Om startIndex är större än Array.Lengthgenererar metoden en ArgumentOutOfRangeException.
Den här metoden är en O(n)-åtgärd, där n är antalet element från startIndex till slutet av array.
Se även
Gäller för
IndexOf<T>(T[], T, Int32, Int32)
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
Söker efter det angivna objektet i ett område med element i en endimensionell matris och returnerar indexet för den första förekomsten. Intervallet sträcker sig från ett angivet index för ett angivet antal element.
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
Typparametrar
- T
Typ av element i matrisen.
Parametrar
- array
- T[]
Den endimensionella, nollbaserade matrisen som ska sökas.
- value
- T
Objektet som ska hittas i array.
- startIndex
- Int32
Det nollbaserade startindexet för sökningen. 0 (noll) är giltigt i en tom matris.
- count
- Int32
Antalet element i avsnittet som ska sökas.
Returer
Det nollbaserade indexet för den första förekomsten av value inom området för element i array som börjar vid startIndex och innehåller antalet element som anges i count, om det hittas, annars -1.
Undantag
array är null.
startIndex ligger utanför intervallet för giltiga index för array.
-eller-
count är mindre än noll.
-eller-
startIndex och count ange inte ett giltigt avsnitt i array.
Exempel
I följande exempel visas alla tre allmänna överlagringar av IndexOf metoden. En matris med strängar skapas, med en post som visas två gånger, på indexplats 0 och indexplats 5. Metodens IndexOf<T>(T[], T) överlagring söker i matrisen från början och hittar den första förekomsten av strängen. Metodöverlagringen IndexOf<T>(T[], T, Int32) används för att söka i matrisen som börjar med indexplats 3 och fortsätta till slutet av matrisen och hittar den andra förekomsten av strängen. Slutligen används metodöverlagringen IndexOf<T>(T[], T, Int32, Int32) för att söka i ett intervall med två poster, som börjar på indexplats två. Den returnerar -1 eftersom det inte finns några instanser av söksträngen i det intervallet.
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
Kommentarer
Den här metoden söker igenom elementen i en endimensionell matris från startIndex till startIndex plus count minus 1, om count är större än 0. För att avgöra om value finns i arrayutför metoden en likhetsjämförelse med hjälp av standardjämförelsejämförelsen EqualityComparer<T>.Default.
Om startIndex är lika med returnerar Array.Lengthmetoden -1. Om startIndex är större än Array.Lengthgenererar metoden en ArgumentOutOfRangeException.
Den här metoden är en O()-nåtgärd, där n är count.
Se även
Gäller för
IndexOf<T>(T[], T)
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
- Källa:
- Array.cs
Söker efter det angivna objektet och returnerar indexet för dess första förekomst i en endimensionell matris.
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
Typparametrar
- T
Typ av element i matrisen.
Parametrar
- array
- T[]
Den endimensionella, nollbaserade matrisen som ska sökas.
- value
- T
Objektet som ska hittas i array.
Returer
Det nollbaserade indexet för den första förekomsten av value i hela array, om det hittas, annars -1.
Undantag
array är null.
Exempel
I följande exempel visas alla tre allmänna överlagringar av IndexOf metoden. En matris med strängar skapas, med en post som visas två gånger, på indexplats 0 och indexplats 5. Metodens IndexOf<T>(T[], T) överlagring söker i matrisen från början och hittar den första förekomsten av strängen. Metodöverlagringen IndexOf<T>(T[], T, Int32) används för att söka i matrisen som börjar med indexplats 3 och fortsätta till slutet av matrisen och hittar den andra förekomsten av strängen. Slutligen används metodöverlagringen IndexOf<T>(T[], T, Int32, Int32) för att söka i ett intervall med två poster, som börjar på indexplats två. Den returnerar -1 eftersom det inte finns några instanser av söksträngen i det intervallet.
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
Kommentarer
Den här metoden söker igenom alla element i en endimensionell matris efter value. För att avgöra om value finns i arrayutför metoden en likhetsjämförelse med hjälp av standardjämförelsejämförelsen EqualityComparer<T>.Default.
Den här metoden är en O()-nåtgärd, där n är för Lengtharray.