Array.LastIndexOf 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í.
Přetížení
LastIndexOf(Array, Object) |
Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého jednorozměrného Arrayobjektu . |
LastIndexOf(Array, Object, Int32) |
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v jednorozměrném Array objektu, který se rozšiřuje od prvního prvku k zadanému indexu. |
LastIndexOf(Array, Object, Int32, Int32) |
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v jednorozměrném Array objektu, který obsahuje zadaný počet prvků a končí na zadaném indexu. |
LastIndexOf<T>(T[], T) |
Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého Arrayobjektu . |
LastIndexOf<T>(T[], T, Int32) |
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v objektu Array , který se rozšiřuje od prvního prvku k zadanému indexu. |
LastIndexOf<T>(T[], T, Int32, Int32) |
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v objektu Array , který obsahuje zadaný počet prvků a končí na zadaném indexu. |
LastIndexOf(Array, Object)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého jednorozměrného Arrayobjektu .
public:
static int LastIndexOf(Array ^ array, System::Object ^ value);
public static int LastIndexOf (Array array, object value);
public static int LastIndexOf (Array array, object? value);
static member LastIndexOf : Array * obj -> int
Public Shared Function LastIndexOf (array As Array, value As Object) As Integer
Parametry
- value
- Object
Objekt, který se má vyhledat v array
.
Návraty
Index posledního výskytu value
v rámci celého array
objektu , pokud je nalezen; jinak dolní mez pole mínus 1.
Výjimky
array
je null
.
Pole array
je multidimenzionální.
Příklady
Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku v poli.
using namespace System;
void PrintIndexAndValues( Array^ myArray );
void main()
{
// Creates and initializes a new Array instance with three elements of the same value.
Array^ myArray = Array::CreateInstance( String::typeid, 12 );
myArray->SetValue( "the", 0 );
myArray->SetValue( "quick", 1 );
myArray->SetValue( "brown", 2 );
myArray->SetValue( "fox", 3 );
myArray->SetValue( "jumps", 4 );
myArray->SetValue( "over", 5 );
myArray->SetValue( "the", 6 );
myArray->SetValue( "lazy", 7 );
myArray->SetValue( "dog", 8 );
myArray->SetValue( "in", 9 );
myArray->SetValue( "the", 10 );
myArray->SetValue( "barn", 11 );
// Displays the values of the Array.
Console::WriteLine( "The Array instance contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
String^ myString = "the";
int myIndex = Array::LastIndexOf( myArray, myString );
Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array::LastIndexOf( myArray, myString, 8 );
Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
Console::WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}
void PrintIndexAndValues( Array^ myArray )
{
for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
Console::WriteLine( "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}
/*
This code produces the following output.
The Array instance 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// This code produces 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 last occurrence of "the" is at index 10.
// The last occurrence of "the" between the start and index 8 is at index 6.
// The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}
/*
This code produces 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first
' section of the Array.
myIndex = Array.LastIndexOf(myArray, myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start " _
+ "and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section
' of the Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces 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 last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.
Poznámky
Jednorozměrný Array je prohledána zpět od posledního prvku a končí na prvním prvku.
Prvky se porovnávají se zadanou hodnotou pomocí Object.Equals metody . Pokud je typ elementu neintrinsický (definovaný uživatelem), použije se Equals
implementace tohoto typu.
Vzhledem k tomu, že většina polí bude mít dolní mez nuly, 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 a value
není nalezena, vrátí tato metoda hodnotu Int32.MaxValue, což je System.Int32.MinValue - 1
.
Tato metoda je operace O(n
), kde n
je hodnota Length .array
V rozhraní .NET Framework 2.0 a novějších verzích tato metoda používá Equals metody Array a CompareTo k určení, zda Object existuje parametr určený parametrem value
. V dřívějších verzích rozhraní .NET Framework bylo toto určení provedeno pomocí Equals metod a CompareTo samotného value
Object rozhraní .
CompareTo metody parametru item
u objektů v kolekci.
Viz také
Platí pro
LastIndexOf(Array, Object, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v jednorozměrném Array objektu, který se rozšiřuje od prvního prvku k zadanému indexu.
public:
static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int LastIndexOf (Array array, object value, int startIndex);
public static int LastIndexOf (Array array, object? value, int startIndex);
static member LastIndexOf : Array * obj * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer) As Integer
Parametry
- value
- Object
Objekt, který se má vyhledat v array
.
- startIndex
- Int32
Počáteční index zpětného vyhledávání.
Návraty
Index posledního výskytu value
prvku v rozsahu prvků v array
, který se rozšiřuje od prvního prvku do , pokud je startIndex
nalezen; 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
Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku v poli.
using namespace System;
void PrintIndexAndValues( Array^ myArray );
void main()
{
// Creates and initializes a new Array instance with three elements of the same value.
Array^ myArray = Array::CreateInstance( String::typeid, 12 );
myArray->SetValue( "the", 0 );
myArray->SetValue( "quick", 1 );
myArray->SetValue( "brown", 2 );
myArray->SetValue( "fox", 3 );
myArray->SetValue( "jumps", 4 );
myArray->SetValue( "over", 5 );
myArray->SetValue( "the", 6 );
myArray->SetValue( "lazy", 7 );
myArray->SetValue( "dog", 8 );
myArray->SetValue( "in", 9 );
myArray->SetValue( "the", 10 );
myArray->SetValue( "barn", 11 );
// Displays the values of the Array.
Console::WriteLine( "The Array instance contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
String^ myString = "the";
int myIndex = Array::LastIndexOf( myArray, myString );
Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array::LastIndexOf( myArray, myString, 8 );
Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
Console::WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}
void PrintIndexAndValues( Array^ myArray )
{
for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
Console::WriteLine( "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}
/*
This code produces the following output.
The Array instance 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// This code produces 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 last occurrence of "the" is at index 10.
// The last occurrence of "the" between the start and index 8 is at index 6.
// The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}
/*
This code produces 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first
' section of the Array.
myIndex = Array.LastIndexOf(myArray, myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start " _
+ "and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section
' of the Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces 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 last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.
Poznámky
Jednorozměrná Array je prohledána zpět počínaje startIndex
a končící od prvního prvku.
Prvky se porovnávají se zadanou hodnotou pomocí Object.Equals metody . Pokud je typ elementu neintrinsický (definovaný uživatelem), použije se Equals
implementace tohoto typu.
Vzhledem k tomu, že většina polí bude mít dolní mez nuly, 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 a value
není nalezena, vrátí tato metoda hodnotu Int32.MaxValue, což je System.Int32.MinValue - 1
.
Tato metoda je operace O(n
), kde n
je počet prvků od začátku array
do startIndex
.
V rozhraní .NET Framework 2.0 a novějších verzích tato metoda používá Equals metody Array a CompareTo k určení, zda Object existuje parametr určený parametrem value
. V dřívějších verzích rozhraní .NET Framework bylo toto určení provedeno pomocí Equals metod a CompareTo samotného value
Object rozhraní .
Viz také
Platí pro
LastIndexOf(Array, Object, Int32, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v jednorozměrném Array objektu, který obsahuje zadaný počet prvků a končí na zadaném indexu.
public:
static int LastIndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int LastIndexOf (Array array, object value, int startIndex, int count);
public static int LastIndexOf (Array array, object? value, int startIndex, int count);
static member LastIndexOf : Array * obj * int * int -> int
Public Shared Function LastIndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer
Parametry
- value
- Object
Objekt, který se má vyhledat v array
.
- startIndex
- Int32
Počáteční index zpětného vyhledávání.
- count
- Int32
Počet prvků v prohledávané části.
Návraty
Index posledního výskytu value
prvku v rozsahu prvků v array
, který obsahuje počet prvků zadaných v count
a končí na startIndex
, pokud je nalezen; jinak 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 array
souboru .
Pole array
je multidimenzionální.
Příklady
Následující příklad kódu ukazuje, jak určit index posledního výskytu zadaného prvku v poli. Všimněte si, že LastIndexOf metoda je zpětné hledání; count
proto musí být menší než nebo rovno (startIndex
minus dolní mez pole plus 1).
using namespace System;
void PrintIndexAndValues( Array^ myArray );
void main()
{
// Creates and initializes a new Array instance with three elements of the same value.
Array^ myArray = Array::CreateInstance( String::typeid, 12 );
myArray->SetValue( "the", 0 );
myArray->SetValue( "quick", 1 );
myArray->SetValue( "brown", 2 );
myArray->SetValue( "fox", 3 );
myArray->SetValue( "jumps", 4 );
myArray->SetValue( "over", 5 );
myArray->SetValue( "the", 6 );
myArray->SetValue( "lazy", 7 );
myArray->SetValue( "dog", 8 );
myArray->SetValue( "in", 9 );
myArray->SetValue( "the", 10 );
myArray->SetValue( "barn", 11 );
// Displays the values of the Array.
Console::WriteLine( "The Array instance contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
String^ myString = "the";
int myIndex = Array::LastIndexOf( myArray, myString );
Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array::LastIndexOf( myArray, myString, 8 );
Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
Console::WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}
void PrintIndexAndValues( Array^ myArray )
{
for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
Console::WriteLine( "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}
/*
This code produces the following output.
The Array instance 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
let printIndexAndValues (arr: 'a []) =
for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
printfn $"\t[{i}]:\t{arr[i]}"
// Creates and initializes a new Array with three elements of the same value.
let myArray =
[| "the"; "quick"; "brown"; "fox"
"jumps"; "over"; "the"; "lazy"
"dog"; "in"; "the"; "barn" |]
// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray
// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."
// This code produces 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 last occurrence of "the" is at index 10.
// The last occurrence of "the" between the start and index 8 is at index 6.
// The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );
// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );
// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
void PrintIndexAndValues( Array anArray ) {
for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}
/*
This code produces 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array with three elements of
' the same value.
Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
myArray.SetValue("the", 0)
myArray.SetValue("quick", 1)
myArray.SetValue("brown", 2)
myArray.SetValue("fox", 3)
myArray.SetValue("jumps", 4)
myArray.SetValue("over", 5)
myArray.SetValue("the", 6)
myArray.SetValue("lazy", 7)
myArray.SetValue("dog", 8)
myArray.SetValue("in", 9)
myArray.SetValue("the", 10)
myArray.SetValue("barn", 11)
' Displays the values of the Array.
Console.WriteLine("The Array contains the following values:")
PrintIndexAndValues(myArray)
' Searches for the last occurrence of the duplicated value.
Dim myString As String = "the"
Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
myString, myIndex)
' Searches for the last occurrence of the duplicated value in the first
' section of the Array.
myIndex = Array.LastIndexOf(myArray, myString, 8)
Console.WriteLine("The last occurrence of ""{0}"" between the start " _
+ "and index 8 is at index {1}.", myString, myIndex)
' Searches for the last occurrence of the duplicated value in a section
' of the Array. Note that the start index is greater than the end
' index because the search is done backward.
myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
+ "and index 10 is at index {1}.", myString, myIndex)
End Sub
Public Shared Sub PrintIndexAndValues(myArray As Array)
Dim i As Integer
For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myArray.GetValue(i))
Next i
End Sub
End Class
' This code produces 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 last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.
Poznámky
Jednorozměrná Array hodnota je prohledána zpět od a končící na startIndex
startIndex
mínus count
plus 1, pokud count
je větší než 0.
Prvky se porovnávají se zadanou hodnotou pomocí Object.Equals metody . Pokud je typ elementu neintrinsický (definovaný uživatelem), použije seEquals
implementace tohoto typu.
Vzhledem k tomu, že většina polí bude mít dolní mez nuly, 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 a value
není nalezena, vrátí tato metoda hodnotu Int32.MaxValue, což je System.Int32.MinValue - 1
.
Tato metoda je operace O(n
), kde n
je count
.
V rozhraní .NET Framework 2.0 a novějších verzích tato metoda používá Equals metody Array a CompareTo k určení, zda Object existuje parametr určený parametrem value
. V dřívějších verzích rozhraní .NET Framework bylo toto určení provedeno pomocí Equals metod a CompareTo samotného value
Object rozhraní .
Viz také
Platí pro
LastIndexOf<T>(T[], T)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index posledního výskytu v rámci celého Arrayobjektu .
public:
generic <typename T>
static int LastIndexOf(cli::array <T> ^ array, T value);
public static int LastIndexOf<T> (T[] array, T value);
static member LastIndexOf : 'T[] * 'T -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T) As Integer
Parametry typu
- T
Typ prvků pole.
Parametry
- array
- T[]
Jednorozměrné vyhledávání založené na Array nule.
- value
- T
Objekt, který se má vyhledat v array
.
Návraty
Index od nuly posledního výskytu v rámci celého array
objektu value
, pokud je nalezen; jinak hodnota -1.
Výjimky
array
je null
.
Příklady
Následující příklad kódu ukazuje všechny tři obecná přetížení LastIndexOf 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í LastIndexOf<T>(T[], T) metody prohledá celé pole od konce a najde druhý výskyt řetězce. Přetížení LastIndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole zpět, počínaje umístěním indexu 3 a pokračuje na začátek pole a najde první výskyt řetězce. LastIndexOf<T>(T[], T, Int32, Int32) Nakonec přetížení metody slouží k prohledávání rozsahu čtyř položek, počínaje indexem umístění 4 a rozšířením dozadu (to znamená, že prohledá položky v umístěních 4, 3, 2 a 1); toto hledání vrátí -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.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console::WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console::WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -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.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Poznámky
Prohledá Array se zpět od posledního prvku a končící na prvním prvku.
Prvky se porovnávají se zadanou hodnotou pomocí Object.Equals metody . Pokud je typ elementu neintrinsický (definovaný uživatelem), použije se Equals
implementace tohoto typu.
Tato metoda je operace O(n
), kde n
je hodnota Length .array
Viz také
Platí pro
LastIndexOf<T>(T[], T, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v objektu Array , který se rozšiřuje od prvního prvku k zadanému indexu.
public:
generic <typename T>
static int LastIndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int LastIndexOf<T> (T[] array, T value, int startIndex);
static member LastIndexOf : 'T[] * 'T * int -> int
Public Shared Function LastIndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer
Parametry typu
- T
Typ prvků pole.
Parametry
- array
- T[]
Jednorozměrné vyhledávání založené na Array nule.
- value
- T
Objekt, který se má vyhledat v array
.
- startIndex
- Int32
Počáteční index zpětného vyhledávání začínající od nuly.
Návraty
Nulový index posledního výskytu value
prvku v rozsahu prvků v array
, který se rozšiřuje od prvního prvku do , pokud je startIndex
nalezen; jinak -1.
Výjimky
array
je null
.
startIndex
je mimo rozsah platných indexů pro array
.
Příklady
Následující příklad kódu ukazuje všechny tři obecná přetížení LastIndexOf 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í LastIndexOf<T>(T[], T) metody prohledá celé pole od konce a najde druhý výskyt řetězce. Přetížení LastIndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole zpět, počínaje umístěním indexu 3 a pokračuje na začátek pole a najde první výskyt řetězce. LastIndexOf<T>(T[], T, Int32, Int32) Nakonec přetížení metody slouží k prohledávání rozsahu čtyř položek, počínaje indexem umístění 4 a rozšířením dozadu (to znamená, že prohledá položky v umístěních 4, 3, 2 a 1); toto hledání vrátí -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.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console::WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console::WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -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.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Poznámky
Prohledá Array se zpět od startIndex
prvního prvku a končící v prvním prvku.
Prvky se porovnávají se zadanou hodnotou pomocí Object.Equals metody . Pokud je typ elementu neintrinsický (definovaný uživatelem), použije se Equals
implementace tohoto typu.
Tato metoda je operace O(n
), kde n
je počet prvků od začátku array
do startIndex
.
Viz také
Platí pro
LastIndexOf<T>(T[], T, Int32, Int32)
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
- Zdroj:
- Array.cs
Vyhledá zadaný objekt a vrátí index posledního výskytu v rozsahu prvků v objektu Array , který obsahuje zadaný počet prvků a končí na zadaném indexu.
public:
generic <typename T>
static int LastIndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int LastIndexOf<T> (T[] array, T value, int startIndex, int count);
static member LastIndexOf : 'T[] * 'T * int * int -> int
Public Shared Function LastIndexOf(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é vyhledávání založené na Array nule.
- value
- T
Objekt, který se má vyhledat v array
.
- startIndex
- Int32
Počáteční index zpětného vyhledávání začínající od nuly.
- count
- Int32
Počet prvků v prohledávané části.
Návraty
Index posledního výskytu prvku v rámci rozsahu value
prvků v, který obsahuje počet prvků zadaných v array
count
a končí v startIndex
, pokud je nalezen, v opačném případě hodnota -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 array
souboru .
Příklady
Následující příklad kódu ukazuje všechny tři obecná přetížení LastIndexOf 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í LastIndexOf<T>(T[], T) metody prohledá celé pole od konce a najde druhý výskyt řetězce. Přetížení LastIndexOf<T>(T[], T, Int32) metody se používá k vyhledávání pole zpět, počínaje umístěním indexu 3 a pokračuje na začátek pole a najde první výskyt řetězce. LastIndexOf<T>(T[], T, Int32, Int32) Nakonec přetížení metody slouží k prohledávání rozsahu čtyř položek, počínaje indexem umístění 4 a rozšířením dozadu (to znamená, že prohledá položky v umístěních 4, 3, 2 a 1); toto hledání vrátí -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.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console::WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console::WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
/* This code example produces the following output:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System
let dinosaurs =
[| "Tyrannosaurus"
"Amargasaurus"
"Mamenchisaurus"
"Brachiosaurus"
"Deinonychus"
"Tyrannosaurus"
"Compsognathus" |]
printfn ""
for dino in dinosaurs do
printfn $"{dino}"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4)
|> printfn "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): %i"
// This code example produces the following output:
//
// Tyrannosaurus
// Amargasaurus
// Mamenchisaurus
// Brachiosaurus
// Deinonychus
// Tyrannosaurus
// Compsognathus
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
// Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -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.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3))
Console.WriteLine(vbLf & _
"Array.LastIndexOf(dinosaurs, ""Tyrannosaurus"", 4, 4): {0}", _
Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4))
End Sub
End Class
' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
Poznámky
Prohledá Array se zpět od a končící na startIndex
startIndex
mínus count
plus 1, pokud count
je větší než 0.
Prvky se porovnávají se zadanou hodnotou pomocí Object.Equals metody . Pokud je typ elementu neintrinsický (definovaný uživatelem), použije se Equals
implementace tohoto typu.
Tato metoda je operace O(n
), kde n
je count
.
Viz také
Platí pro
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro