Array.IndexOf Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Vyhledá zadaný objekt a vrátí index jeho prvního výskytu v jednorozměrném poli nebo v oblasti prvků v poli.
Přetížení
| IndexOf(Array, Object) |
Vyhledá zadaný objekt a vrátí index jeho prvního výskytu v jednorozměrném poli. |
| IndexOf(Array, Object, Int32) |
Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index jeho prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole. |
| IndexOf(Array, Object, Int32, Int32) |
Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu ifs. Rozsah se rozšiřuje od zadaného indexu pro zadaný počet prvků. |
| IndexOf<T>(T[], T, Int32) |
Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index jeho prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole. |
| IndexOf<T>(T[], T, Int32, Int32) |
Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index jeho prvního výskytu. Rozsah se rozšiřuje od zadaného indexu pro zadaný počet prvků. |
| IndexOf<T>(T[], T) |
Vyhledá zadaný objekt a vrátí index jeho prvního výskytu v jednorozměrném poli. |
IndexOf(Array, Object)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index jeho prvního výskytu v jednorozměrném poli.
public:
static int IndexOf(Array ^ array, System::Object ^ value);
public static int IndexOf (Array array, object value);
public static int IndexOf (Array array, object? value);
static member IndexOf : Array * obj -> int
Public Shared Function IndexOf (array As Array, value As Object) As Integer
Parametry
- array
- Array
Jednorozměrné pole, které se má hledat.
- value
- Object
Objekt, který se má vyhledat v array.
Návraty
Index prvního výskytu v valuearray, pokud je nalezen; jinak dolní mez pole mínus 1.
Výjimky
array je null.
Pole array je multidimenzionální.
Příklady
Příklad volá následující tři přetížení IndexOf metody k vyhledání indexu řetězce v řetězcovém poli:
IndexOf(Array, Object)k určení prvního výskytu řetězce "the" v řetězcovém poli.
IndexOf(Array, Object, Int32)k určení prvního výskytu řetězce "the" ve čtvrtém až posledním prvku pole řetězců.
IndexOf(Array, Object, Int32, Int32)k určení prvního výskytu řetězce "the" v řetězcovém poli z elementu, který následuje po poslední úspěšnou shodu na konec pole.
using namespace System;
void main()
{
// Create a string array with 3 elements having the same value.
array<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.
// Create a string array with 3 elements having the same value.
let strings =
[| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
"the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
printfn $" [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays the following output:
// The array contains the following values:
// [ 0]: the
// [ 1]: quick
// [ 2]: brown
// [ 3]: fox
// [ 4]: jumps
// [ 5]: over
// [ 6]: the
// [ 7]: lazy
// [ 8]: dog
// [ 9]: in
// [10]: the
// [11]: barn
// The first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index);
// The example displays the following output:
// The array contains the following values:
// [ 0]: the
// [ 1]: quick
// [ 2]: brown
// [ 3]: fox
// [ 4]: jumps
// [ 5]: over
// [ 6]: the
// [ 7]: lazy
// [ 8]: dog
// [ 9]: in
// [10]: the
// [11]: barn
// The first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = Array.IndexOf(strings, searchString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in a section of the array.
Dim position As Integer = index + 1
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index)
End Sub
End Module
' The example displays the following output:
' The array contains the following values:
' [ 0]: the
' [ 1]: quick
' [ 2]: brown
' [ 3]: fox
' [ 4]: jumps
' [ 5]: over
' [ 6]: the
' [ 7]: lazy
' [ 8]: dog
' [ 9]: in
' [10]: the
' [11]: barn
' The first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 7 and index 11 is at index 10.
Poznámky
Tato metoda prohledá všechny prvky jednorozměrného pole pro value. Chcete-li zjistit, zda value existuje v array, provádí metoda porovnání rovnosti voláním metody každého prvku Equals , dokud nenajde shodu. To znamená, že pokud element přepíše metodu Object.Equals(Object) , je toto přepsání volána.
Vzhledem k tomu, že většina polí má dolní mez nula, vrátí tato metoda obecně hodnotu -1, pokudvalue není nalezena. Ve výjimečných případech, kdy je dolní mez pole rovna Int32.MinValue(0x80000000) a value není nalezena, vrátí tato metoda ( Int32.MaxValue 0x7FFFFFFF).
Tato metoda je operace O(n), kde n je hodnota Length .array
Viz také
Platí pro
IndexOf(Array, Object, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index jeho prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole.
public:
static int IndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int IndexOf (Array array, object value, int startIndex);
public static int IndexOf (Array array, object? value, int startIndex);
static member IndexOf : Array * obj * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer) As Integer
Parametry
- array
- Array
Jednorozměrné pole, které se má hledat.
- value
- Object
Objekt, který se má vyhledat v array.
- startIndex
- Int32
Počáteční index vyhledávání. V prázdném poli je platná hodnota 0 (nula).
Návraty
Index prvního výskytu objektu value, pokud je nalezen, v rozsahu prvků v array , který přesahuje od startIndex posledního prvku; jinak dolní mez pole mínus 1.
Výjimky
array je null.
startIndex je mimo rozsah platných indexů pro array.
Pole array je multidimenzionální.
Příklady
Příklad volá následující tři přetížení IndexOf metody k vyhledání indexu řetězce v řetězcovém poli:
IndexOf(Array, Object)k určení prvního výskytu řetězce "the" v řetězcovém poli.
IndexOf(Array, Object, Int32)k určení prvního výskytu řetězce "the" ve čtvrtém až posledním prvku pole řetězců.
IndexOf(Array, Object, Int32, Int32)k určení prvního výskytu řetězce "the" v řetězcovém poli z elementu, který následuje po poslední úspěšnou shodu na konec pole.
using namespace System;
void main()
{
// Create a string array with 3 elements having the same value.
array<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.
// Create a string array with 3 elements having the same value.
let strings =
[| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
"the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
printfn $" [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays the following output:
// The array contains the following values:
// [ 0]: the
// [ 1]: quick
// [ 2]: brown
// [ 3]: fox
// [ 4]: jumps
// [ 5]: over
// [ 6]: the
// [ 7]: lazy
// [ 8]: dog
// [ 9]: in
// [10]: the
// [11]: barn
// The first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index);
// The example displays the following output:
// The array contains the following values:
// [ 0]: the
// [ 1]: quick
// [ 2]: brown
// [ 3]: fox
// [ 4]: jumps
// [ 5]: over
// [ 6]: the
// [ 7]: lazy
// [ 8]: dog
// [ 9]: in
// [10]: the
// [11]: barn
// The first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = Array.IndexOf(strings, searchString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in a section of the array.
Dim position As Integer = index + 1
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index)
End Sub
End Module
' The example displays the following output:
' The array contains the following values:
' [ 0]: the
' [ 1]: quick
' [ 2]: brown
' [ 3]: fox
' [ 4]: jumps
' [ 5]: over
' [ 6]: the
' [ 7]: lazy
' [ 8]: dog
' [ 9]: in
' [10]: the
' [11]: barn
' The first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 7 and index 11 is at index 10.
Poznámky
Tato metoda prohledá jednorozměrné pole z elementu v indexu startIndex do posledního elementu. Chcete-li zjistit, zda value existuje v array, provádí metoda porovnání rovnosti voláním Equals metody každého prvku, dokud nenajde shodu. To znamená, že pokud element přepíše metodu Object.Equals(Object) , je toto přepsání volána.
Vzhledem k tomu, že většina polí má dolní mez nula, vrátí tato metoda obecně hodnotu -1, pokud value není nalezena. Ve výjimečných případech, kdy je dolní mez pole rovna Int32.MinValue(0x80000000) a value není nalezena, vrátí tato metoda ( Int32.MaxValue 0x7FFFFFFF).
Pokud startIndex je hodnota rovna Array.Length, vrátí metoda hodnotu -1. Pokud startIndex je větší než Array.Length, metoda vyvolá ArgumentOutOfRangeException.
Tato metoda je operace O(n), kde n je počet prvků od startIndex do konce array.
Viz také
Platí pro
IndexOf(Array, Object, Int32, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt v rozsahu prvků jednorozměrného pole a vrátí index prvního výskytu ifs. Rozsah se rozšiřuje od zadaného indexu pro zadaný počet prvků.
public:
static int IndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int IndexOf (Array array, object value, int startIndex, int count);
public static int IndexOf (Array array, object? value, int startIndex, int count);
static member IndexOf : Array * obj * int * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer
Parametry
- array
- Array
Jednorozměrné pole, které se má hledat.
- value
- Object
Objekt, který se má vyhledat v array.
- startIndex
- Int32
Počáteční index vyhledávání. V prázdném poli je platná hodnota 0 (nula).
- count
- Int32
Počet prvků, které se mají prohledat.
Návraty
Index prvního výskytu objektu value, pokud je nalezen v indexu startIndexarray od do startIndex + count - 1; v opačném případě je dolní mez pole mínus 1.
Výjimky
array je null.
startIndex je mimo rozsah platných indexů pro array.
-nebo-
Hodnota count je menší než nula.
-nebo-
startIndex a count nezadávejte platný oddíl v arraysouboru .
Pole array je multidimenzionální.
Příklady
Příklad volá následující tři přetížení IndexOf metody k vyhledání indexu řetězce v řetězcovém poli:
IndexOf(Array, Object)k určení prvního výskytu řetězce "the" v řetězcovém poli.
IndexOf(Array, Object, Int32)k určení prvního výskytu řetězce "the" ve čtvrtém až posledním prvku pole řetězců.
IndexOf(Array, Object, Int32, Int32)k určení prvního výskytu řetězce "the" v řetězcovém poli z elementu, který následuje po poslední úspěšnou shodu na konec pole. K určení hodnoty argumentu
countodečte horní mez pole od počátečního indexu a přičte jednu.
using namespace System;
void main()
{
// Create a string array with 3 elements having the same value.
array<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.
// Create a string array with 3 elements having the same value.
let strings =
[| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
"the"; "lazy"; "dog"; "in"; "the"; "barn" |]
// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
printfn $" [{i,2}]: {strings[i]}"
// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."
// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."
// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}."
// The example displays the following output:
// The array contains the following values:
// [ 0]: the
// [ 1]: quick
// [ 2]: brown
// [ 3]: fox
// [ 4]: jumps
// [ 5]: over
// [ 6]: the
// [ 7]: lazy
// [ 8]: dog
// [ 9]: in
// [10]: the
// [11]: barn
// The first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
string searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index);
// The example displays the following output:
// The array contains the following values:
// [ 0]: the
// [ 1]: quick
// [ 2]: brown
// [ 3]: fox
// [ 4]: jumps
// [ 5]: over
// [ 6]: the
// [ 7]: lazy
// [ 8]: dog
// [ 9]: in
// [10]: the
// [11]: barn
// The first occurrence of "the" is at index 0.
// The first occurrence of "the" between index 4 and the end is at index 6.
// The first occurrence of "the" between index 7 and index 11 is at index 10.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = Array.IndexOf(strings, searchString)
Console.WriteLine("The first occurrence of ""{0}"" is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4)
Console.WriteLine("The first occurrence of ""{0}"" between index 4 and the end is at index {1}.",
searchString, index)
' Search for the first occurrence of the duplicated value in a section of the array.
Dim position As Integer = index + 1
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1)
Console.WriteLine("The first occurrence of ""{0}"" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index)
End Sub
End Module
' The example displays the following output:
' The array contains the following values:
' [ 0]: the
' [ 1]: quick
' [ 2]: brown
' [ 3]: fox
' [ 4]: jumps
' [ 5]: over
' [ 6]: the
' [ 7]: lazy
' [ 8]: dog
' [ 9]: in
' [10]: the
' [11]: barn
' The first occurrence of "the" is at index 0.
' The first occurrence of "the" between index 4 and the end is at index 6.
' The first occurrence of "the" between index 7 and index 11 is at index 10.
Poznámky
Tato metoda prohledá prvky jednorozměrného pole od startIndex do startIndex plus count minus 1, pokud count je větší než 0. Chcete-li zjistit, zda value existuje v array, provádí metoda porovnání rovnosti voláním Equals metody každého prvku, dokud nenajde shodu. To znamená, že pokud element přepíše metodu Object.Equals , je toto přepsání volána.
Vzhledem k tomu, že většina polí má dolní mez nula, vrátí tato metoda obecně hodnotu -1, pokud value není nalezena. Ve výjimečných případech, kdy je dolní mez pole rovna Int32.MinValue (0x80000000) a value není nalezena, vrátí tato metoda ( Int32.MaxValue 0x7FFFFFFF).
Pokud startindex se Array.Lengthrovná , vrátí metoda -1. Pokud startIndex je větší než Array.Length, metoda vyvolá ArgumentOutOfRangeException.
Tato metoda je operace O(n), kde n je count.
Viz také
Platí pro
IndexOf<T>(T[], T, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt v oblasti prvků jednorozměrného pole a vrátí index jeho prvního výskytu. Rozsah se rozšiřuje od zadaného indexu na konec pole.
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int IndexOf<T> (T[] array, T value, int startIndex);
static member IndexOf : 'T[] * 'T * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer
Parametry typu
- T
Typ prvků pole.
Parametry
- array
- T[]
Jednorozměrné pole založené na nule, které se má prohledávat.
- value
- T
Objekt, který se má vyhledat v array.
- startIndex
- Int32
Počáteční index hledání začínající od nuly. V prázdném poli je platná hodnota 0 (nula).
Návraty
Nulový index prvního výskytu v rozsahu value prvků, array který přesahuje od startIndex posledního elementu, pokud je nalezen, v opačném případě -1.
Výjimky
array je null.
startIndex je mimo rozsah platných indexů pro array.
Příklady
Následující příklad ukazuje všechny tři obecná přetížení IndexOf metody. Je vytvořeno pole řetězců, v němž se jedna položka vyskytuje dvakrát, jednou s indexem 0 a podruhé s indexem 5. Přetížení IndexOf<T>(T[], T) metody prohledá pole od začátku a najde první výskyt řetězce. Přetížení IndexOf<T>(T[], T, Int32) metody se používá k prohledání pole počínaje umístěním indexu 3 a pokračováním na konec pole a najde druhý výskyt řetězce. IndexOf<T>(T[], T, Int32, Int32) Nakonec přetížení metody slouží k prohledání rozsahu dvou položek, počínaje umístěním indexu dvě; vrátí hodnotu -1, protože v tomto rozsahu nejsou žádné instance vyhledávacího řetězce.
using namespace System;
void main()
{
array<String^>^ dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console::WriteLine();
for each(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
*/
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Poznámky
Tato metoda prohledá jednorozměrnou matici od elementu na startIndex konci pole. Chcete-li zjistit, zda value existuje v array, metoda provádí porovnání rovnosti voláním T.Equals metody pro každý prvek. To znamená, že pokud T přepíše metodu Equals , je toto přepsání volána.
Pokud startIndex se rovná Length, vrátí metoda hodnotu -1. Pokud startIndex je větší než Array.Length, metoda vyvolá .ArgumentOutOfRangeException
Tato metoda je operace O(n), kde n je počet prvků od startIndex do konce .array
Viz také
Platí pro
IndexOf<T>(T[], T, Int32, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt v oblasti prvků jednorozměrného pole a vrátí index prvního výskytu. Rozsah se rozšiřuje od zadaného indexu pro zadaný počet prvků.
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int IndexOf<T> (T[] array, T value, int startIndex, int count);
static member IndexOf : 'T[] * 'T * int * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer, count As Integer) As Integer
Parametry typu
- T
Typ prvků pole.
Parametry
- array
- T[]
Jednorozměrné pole založené na nule, které se má hledat.
- value
- T
Objekt, který se má najít v array.
- startIndex
- Int32
Počáteční index hledání začínající od nuly. V prázdném poli je platná hodnota 0 (nula).
- count
- Int32
Počet prvků v prohledávané části.
Návraty
Index založený na nule prvního výskytu v rozsahu value prvků, array který začíná na startIndex a obsahuje počet prvků zadaných v count, pokud je nalezen, jinak -1.
Výjimky
array je null.
startIndex je mimo rozsah platných indexů pro array.
-nebo-
Hodnota count je menší než nula.
-nebo-
startIndex a count nezadávejte platný oddíl v arraysouboru .
Příklady
Následující příklad ukazuje všechna tři obecná přetížení IndexOf metody. Je vytvořeno pole řetězců, v němž se jedna položka vyskytuje dvakrát, jednou s indexem 0 a podruhé s indexem 5. Přetížení IndexOf<T>(T[], T) metody prohledá pole od začátku a najde první výskyt řetězce. Přetížení IndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole počínaje umístěním indexu 3 a pokračuje až na konec pole a najde druhý výskyt řetězce. IndexOf<T>(T[], T, Int32, Int32) Nakonec se přetížení metody používá k prohledávání rozsahu dvou položek, počínaje umístěním indexu dvě; vrátí hodnotu -1, protože v této oblasti nejsou žádné instance vyhledávacího řetězce.
using namespace System;
void main()
{
array<String^>^ dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console::WriteLine();
for each(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
*/
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Poznámky
Tato metoda prohledá prvky jednorozměrného pole od startIndex plus startIndexcount minus 1, pokud count je větší než 0. Chcete-li zjistit, zda value existuje v array, metoda provádí porovnání rovnosti voláním T.Equals metody pro každý prvek. To znamená, že pokud T přepíše metodu Equals , je toto přepsání volána.
Pokud startIndex se Array.Lengthrovná , vrátí metoda hodnotu -1. Pokud startIndex je větší než Array.Length, metoda vyvolá .ArgumentOutOfRangeException
Tato metoda je operace O(n), kde n je count.
Viz také
Platí pro
IndexOf<T>(T[], T)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index jeho prvního výskytu v jednorozměrném poli.
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value);
public static int IndexOf<T> (T[] array, T value);
static member IndexOf : 'T[] * 'T -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T) As Integer
Parametry typu
- T
Typ prvků pole.
Parametry
- array
- T[]
Jednorozměrné pole založené na nule, které se má hledat.
- value
- T
Objekt, který se má najít v array.
Návraty
Nulový index prvního výskytu value v celém arrayobjektu , pokud je nalezen; v opačném případě -1.
Výjimky
array je null.
Příklady
Následující příklad ukazuje všechna tři obecná přetížení IndexOf metody. Je vytvořeno pole řetězců, v němž se jedna položka vyskytuje dvakrát, jednou s indexem 0 a podruhé s indexem 5. Přetížení IndexOf<T>(T[], T) metody prohledá pole od začátku a najde první výskyt řetězce. Přetížení IndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole počínaje umístěním indexu 3 a pokračuje až na konec pole a najde druhý výskyt řetězce. IndexOf<T>(T[], T, Int32, Int32) Nakonec se přetížení metody používá k prohledávání rozsahu dvou položek, počínaje umístěním indexu dvě; vrátí hodnotu -1, protože v této oblasti nejsou žádné instance vyhledávacího řetězce.
using namespace System;
void main()
{
array<String^>^ dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console::WriteLine();
for each(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
*/
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
//
// Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Public Class Example
Public Shared Sub Main()
Dim dinosaurs() As String = { "Tyrannosaurus", _
"Amargasaurus", _
"Mamenchisaurus", _
"Brachiosaurus", _
"Deinonychus", _
"Tyrannosaurus", _
"Compsognathus" }
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5
'
'Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
Poznámky
Tato metoda prohledá všechny prvky jednorozměrného pole pro value. Chcete-li zjistit, zda value existuje v array, metoda provádí porovnání rovnosti voláním T.Equals metody pro každý prvek. To znamená, že pokud T přepíše metodu Equals , je toto přepsání volána.
Tato metoda je operace O(n), kde n je hodnota Length .array