Array.LastIndexOf Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Przeciążenia
| Nazwa | Opis |
|---|---|
| LastIndexOf(Array, Object) |
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w obrębie całego jednowymiarowego Arrayobiektu . |
| LastIndexOf(Array, Object, Int32) |
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowym Array , który rozciąga się od pierwszego elementu do określonego indeksu. |
| LastIndexOf(Array, Object, Int32, Int32) |
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowym Array , który zawiera określoną liczbę elementów i kończy się na określonym indeksie. |
| LastIndexOf<T>(T[], T) |
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w całym Arrayobiekcie . |
| LastIndexOf<T>(T[], T, Int32) |
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w obiekcie Array , który rozciąga się od pierwszego elementu do określonego indeksu. |
| LastIndexOf<T>(T[], T, Int32, Int32) |
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w obiekcie Array zawierającym określoną liczbę elementów i kończy się na określonym indeksie. |
LastIndexOf(Array, Object)
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w obrębie całego jednowymiarowego Arrayobiektu .
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
Obiekt do zlokalizowania w pliku array.
Zwraca
Indeks ostatniego wystąpienia value w całym arrayobiekcie , jeśli zostanie znaleziony; w przeciwnym razie dolna granica tablicy minus 1.
Wyjątki
Parametr array ma wartość null.
array jest wielowymiarowa.
Przykłady
Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu w tablicy.
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.
Uwagi
Jednowymiarowe Array jest przeszukiwane do tyłu, zaczynając od ostatniego elementu i kończąc na pierwszym elemecie.
Elementy są porównywane z określoną wartością Object.Equals przy użyciu metody . Jeśli typ elementu jest typem nonintrinsic (zdefiniowanym przez użytkownika), jest używana implementacja Equals tego typu.
Ponieważ większość tablic ma dolną granicę zera, ta metoda zwykle zwraca -1, gdy value nie zostanie znaleziona. W rzadkich przypadkach, gdy dolna granica tablicy jest równa Int32.MinValue i value nie zostanie znaleziona, ta metoda zwraca Int32.MaxValuewartość , czyli System.Int32.MinValue - 1.
Ta metoda jest operacją O(), gdzie n jest operacją nLength.array
Ta metoda używa Equals metod i CompareTo , Array aby określić, czy Object określony przez value parametr istnieje.
CompareTo metody parametru item w obiektach w kolekcji.
Zobacz też
Dotyczy
LastIndexOf(Array, Object, Int32)
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowym Array , który rozciąga się od pierwszego elementu do określonego indeksu.
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
Obiekt do zlokalizowania w pliku array.
- startIndex
- Int32
Indeks początkowy wyszukiwania wstecznego.
Zwraca
Indeks ostatniego wystąpienia value w zakresie elementów w array tym rozciąga się od pierwszego elementu do startIndex, jeśli zostanie znaleziony; w przeciwnym razie dolna granica tablicy minus 1.
Wyjątki
Parametr array ma wartość null.
startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.
array jest wielowymiarowa.
Przykłady
Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu w tablicy.
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.
Uwagi
Jednowymiarowe Array jest przeszukiwane wstecz, zaczynając od startIndex i kończąc na pierwszym elemecie.
Elementy są porównywane z określoną wartością Object.Equals przy użyciu metody . Jeśli typ elementu jest typem nonintrinsic (zdefiniowanym przez użytkownika), jest używana implementacja Equals tego typu.
Ponieważ większość tablic ma dolną granicę zera, ta metoda zwykle zwraca -1, gdy value nie zostanie znaleziona. W rzadkich przypadkach, gdy dolna granica tablicy jest równa Int32.MinValue i value nie zostanie znaleziona, ta metoda zwraca Int32.MaxValuewartość , czyli System.Int32.MinValue - 1.
Ta metoda jest operacją O(n), gdzie n jest liczbą elementów od początku array do startIndex.
Ta metoda używa Equals metod i CompareTo , Array aby określić, czy Object określony przez value parametr istnieje.
Zobacz też
Dotyczy
LastIndexOf(Array, Object, Int32, Int32)
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w jednowymiarowym Array , który zawiera określoną liczbę elementów i kończy się na określonym indeksie.
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
Obiekt do zlokalizowania w pliku array.
- startIndex
- Int32
Indeks początkowy wyszukiwania wstecznego.
- count
- Int32
Liczba elementów w sekcji do wyszukania.
Zwraca
Indeks ostatniego wystąpienia value w zakresie elementów, array które zawierają liczbę elementów określonych w count i kończy się na startIndex, jeśli zostanie znaleziona; w przeciwnym razie dolna granica tablicy minus 1.
Wyjątki
Parametr array ma wartość null.
startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.
-lub-
Parametr count ma wartość niższą niż zero.
-lub-
startIndex i count nie należy określać prawidłowej sekcji w pliku array.
array jest wielowymiarowa.
Przykłady
Poniższy przykład kodu pokazuje, jak określić indeks ostatniego wystąpienia określonego elementu w tablicy. Należy zauważyć, że LastIndexOf metoda jest wyszukiwaniem wstecznym, count dlatego musi być mniejsza lub równa (startIndex minus dolna granica tablicy plus 1).
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.
Uwagi
Jednowymiarowe Array jest wyszukiwane do tyłu, zaczynając od startIndex i kończąc na startIndex minus count plus 1, jeśli count jest większe niż 0.
Elementy są porównywane z określoną wartością Object.Equals przy użyciu metody . Jeśli typ elementu jest typem nonintrinsic (zdefiniowanym przez użytkownika), jest używana implementacjaEquals tego typu.
Ponieważ większość tablic ma dolną granicę zera, ta metoda zwykle zwraca -1, gdy value nie zostanie znaleziona. W rzadkich przypadkach, gdy dolna granica tablicy jest równa Int32.MinValue i value nie zostanie znaleziona, ta metoda zwraca Int32.MaxValuewartość , czyli System.Int32.MinValue - 1.
Ta metoda jest operacją O(n), gdzie n to count.
Ta metoda używa Equals metod i CompareTo , Array aby określić, czy Object określony przez value parametr istnieje.
Zobacz też
Dotyczy
LastIndexOf<T>(T[], T)
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w całym Arrayobiekcie .
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 elementów tablicy.
Parametry
- array
- T[]
Jednowymiarowe, zero Array oparte na wyszukiwaniu.
- value
- T
Obiekt do zlokalizowania w pliku array.
Zwraca
Indeks zerowy ostatniego wystąpienia value w całym arrayobiekcie , jeśli zostanie znaleziony; w przeciwnym razie -1.
Wyjątki
Parametr array ma wartość null.
Przykłady
Poniższy przykład kodu przedstawia wszystkie trzy ogólne przeciążenia LastIndexOf metody. Zostanie utworzona tablica ciągów z jednym wpisem, który pojawia się dwa razy, w lokalizacji indeksu 0 i lokalizacji indeksu 5. Metoda LastIndexOf<T>(T[], T) przeszukuje całą tablicę z końca i znajduje drugie wystąpienie ciągu. Przeciążenie LastIndexOf<T>(T[], T, Int32) metody służy do wyszukiwania tablicy do tyłu od lokalizacji indeksu 3 i kontynuowania do początku tablicy i znajduje pierwsze wystąpienie ciągu. LastIndexOf<T>(T[], T, Int32, Int32) Na koniec przeciążenie metody służy do wyszukiwania zakresu czterech wpisów, zaczynając od lokalizacji indeksu 4 i rozszerzając się do tyłu (czyli wyszukuje elementy w lokalizacjach 4, 3, 2 i 1); to wyszukiwanie zwraca -1, ponieważ nie ma wystąpień ciągu wyszukiwania w tym zakresie.
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
Uwagi
Element Array jest wyszukiwany do tyłu, zaczynając od ostatniego elementu i kończąc na pierwszym elemecie.
Elementy są porównywane z określoną wartością Object.Equals przy użyciu metody . Jeśli typ elementu jest typem nonintrinsic (zdefiniowanym przez użytkownika), jest używana implementacja Equals tego typu.
Ta metoda jest operacją O(), gdzie n jest operacją nLength.array
Zobacz też
Dotyczy
LastIndexOf<T>(T[], T, Int32)
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w obiekcie Array , który rozciąga się od pierwszego elementu do określonego indeksu.
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 elementów tablicy.
Parametry
- array
- T[]
Jednowymiarowe, zero Array oparte na wyszukiwaniu.
- value
- T
Obiekt do zlokalizowania w pliku array.
- startIndex
- Int32
Zerowy indeks początkowy wyszukiwania wstecznego.
Zwraca
Indeks oparty na zera ostatniego wystąpienia value w zakresie elementów w array tym rozciąga się od pierwszego elementu do startIndex, jeśli zostanie znaleziony; w przeciwnym razie - 1.
Wyjątki
Parametr array ma wartość null.
startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.
Przykłady
Poniższy przykład kodu przedstawia wszystkie trzy ogólne przeciążenia LastIndexOf metody. Zostanie utworzona tablica ciągów z jednym wpisem, który pojawia się dwa razy, w lokalizacji indeksu 0 i lokalizacji indeksu 5. Metoda LastIndexOf<T>(T[], T) przeszukuje całą tablicę z końca i znajduje drugie wystąpienie ciągu. Przeciążenie LastIndexOf<T>(T[], T, Int32) metody służy do wyszukiwania tablicy do tyłu od lokalizacji indeksu 3 i kontynuowania do początku tablicy i znajduje pierwsze wystąpienie ciągu. LastIndexOf<T>(T[], T, Int32, Int32) Na koniec przeciążenie metody służy do wyszukiwania zakresu czterech wpisów, zaczynając od lokalizacji indeksu 4 i rozszerzając się do tyłu (czyli wyszukuje elementy w lokalizacjach 4, 3, 2 i 1); to wyszukiwanie zwraca -1, ponieważ nie ma wystąpień ciągu wyszukiwania w tym zakresie.
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
Uwagi
Element Array jest wyszukiwany do tyłu rozpoczynający się od startIndex i kończący się na pierwszym elemecie.
Elementy są porównywane z określoną wartością Object.Equals przy użyciu metody . Jeśli typ elementu jest typem nonintrinsic (zdefiniowanym przez użytkownika), jest używana implementacja Equals tego typu.
Ta metoda jest operacją O(n), gdzie n jest liczbą elementów od początku array do startIndex.
Zobacz też
Dotyczy
LastIndexOf<T>(T[], T, Int32, Int32)
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
- Źródło:
- Array.cs
Wyszukuje określony obiekt i zwraca indeks ostatniego wystąpienia w zakresie elementów w obiekcie Array zawierającym określoną liczbę elementów i kończy się na określonym indeksie.
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 elementów tablicy.
Parametry
- array
- T[]
Jednowymiarowe, zero Array oparte na wyszukiwaniu.
- value
- T
Obiekt do zlokalizowania w pliku array.
- startIndex
- Int32
Zerowy indeks początkowy wyszukiwania wstecznego.
- count
- Int32
Liczba elementów w sekcji do wyszukania.
Zwraca
Indeks na podstawie zera ostatniego wystąpienia value w zakresie elementów w array pliku, który zawiera liczbę elementów określonych w count i kończy się na startIndex, jeśli zostanie znaleziona; w przeciwnym razie -1.
Wyjątki
Parametr array ma wartość null.
startIndex znajduje się poza zakresem prawidłowych indeksów dla elementu array.
-lub-
Parametr count ma wartość niższą niż zero.
-lub-
startIndex i count nie należy określać prawidłowej sekcji w pliku array.
Przykłady
Poniższy przykład kodu przedstawia wszystkie trzy ogólne przeciążenia LastIndexOf metody. Zostanie utworzona tablica ciągów z jednym wpisem, który pojawia się dwa razy, w lokalizacji indeksu 0 i lokalizacji indeksu 5. Metoda LastIndexOf<T>(T[], T) przeszukuje całą tablicę z końca i znajduje drugie wystąpienie ciągu. Przeciążenie LastIndexOf<T>(T[], T, Int32) metody służy do wyszukiwania tablicy do tyłu od lokalizacji indeksu 3 i kontynuowania do początku tablicy i znajduje pierwsze wystąpienie ciągu. LastIndexOf<T>(T[], T, Int32, Int32) Na koniec przeciążenie metody służy do wyszukiwania zakresu czterech wpisów, zaczynając od lokalizacji indeksu 4 i rozszerzając się do tyłu (czyli wyszukuje elementy w lokalizacjach 4, 3, 2 i 1); to wyszukiwanie zwraca -1, ponieważ nie ma wystąpień ciągu wyszukiwania w tym zakresie.
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
Uwagi
Element Array jest wyszukiwany do tyłu, zaczynając od startIndex i kończąc na startIndex minus count plus 1, jeśli count jest większy niż 0.
Elementy są porównywane z określoną wartością Object.Equals przy użyciu metody . Jeśli typ elementu jest typem nonintrinsic (zdefiniowanym przez użytkownika), jest używana implementacja Equals tego typu.
Ta metoda jest operacją O(n), gdzie n to count.