Array.LastIndexOf Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Overload
LastIndexOf(Array, Object) |
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam seluruh Arraysatu dimensi . |
LastIndexOf(Array, Object, Int32) |
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array satu dimensi yang meluas dari elemen pertama ke indeks yang ditentukan. |
LastIndexOf(Array, Object, Int32, Int32) |
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array satu dimensi yang berisi jumlah elemen yang ditentukan dan berakhir pada indeks yang ditentukan. |
LastIndexOf<T>(T[], T) |
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam seluruh Array. |
LastIndexOf<T>(T[], T, Int32) |
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array yang meluas dari elemen pertama ke indeks yang ditentukan. |
LastIndexOf<T>(T[], T, Int32, Int32) |
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array yang berisi jumlah elemen yang ditentukan dan berakhir pada indeks yang ditentukan. |
LastIndexOf(Array, Object)
- Sumber:
- Array.cs
- Sumber:
- Array.cs
- Sumber:
- Array.cs
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam seluruh Arraysatu dimensi .
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
Parameter
- value
- Object
Objek yang akan ditemukan di array
.
Mengembalikan
Indeks kemunculan terakhir value
dalam seluruh array
, jika ditemukan; jika tidak, batas bawah array dikurangi 1.
Pengecualian
array
null
.
array
multidirmani.
Contoh
Contoh kode berikut menunjukkan cara menentukan indeks kemunculan terakhir elemen tertentu dalam array.
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.
Keterangan
Array satu dimensi dicari mundur mulai dari elemen terakhir dan berakhir pada elemen pertama.
Elemen dibandingkan dengan nilai yang ditentukan menggunakan metode Object.Equals. Jika jenis elemen adalah jenis nonintrinsic (ditentukan pengguna), implementasi Equals
dari jenis tersebut digunakan.
Karena sebagian besar array akan memiliki batas nol yang lebih rendah, metode ini umumnya akan mengembalikan -1 ketika value
tidak ditemukan. Dalam kasus yang jarang terjadi bahwa batas bawah array sama dengan Int32.MinValue dan value
tidak ditemukan, metode ini mengembalikan Int32.MaxValue, yang System.Int32.MinValue - 1
.
Metode ini adalah operasi O(n
), di mana n
adalah Lengtharray
.
Dalam .NET Framework 2.0 dan versi yang lebih baru, metode ini menggunakan metode Equals dan CompareTo dari Array untuk menentukan apakah Object ditentukan oleh parameter value
ada. Dalam versi .NET Framework sebelumnya, penentuan ini dibuat dengan menggunakan metode Equals dan CompareTo dari value
Object itu sendiri.
CompareTo metode parameter item
pada objek dalam koleksi.
Lihat juga
Berlaku untuk
LastIndexOf(Array, Object, Int32)
- Sumber:
- Array.cs
- Sumber:
- Array.cs
- Sumber:
- Array.cs
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array satu dimensi yang meluas dari elemen pertama ke indeks yang ditentukan.
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
Parameter
- value
- Object
Objek yang akan ditemukan di array
.
- startIndex
- Int32
Indeks awal pencarian mundur.
Mengembalikan
Indeks kemunculan terakhir value
dalam rentang elemen dalam array
yang meluas dari elemen pertama ke startIndex
, jika ditemukan; jika tidak, batas bawah array dikurangi 1.
Pengecualian
array
null
.
startIndex
berada di luar rentang indeks yang valid untuk array
.
array
multidirmani.
Contoh
Contoh kode berikut menunjukkan cara menentukan indeks kemunculan terakhir elemen tertentu dalam array.
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.
Keterangan
Array satu dimensi dicari mundur mulai dari startIndex
dan berakhir pada elemen pertama.
Elemen dibandingkan dengan nilai yang ditentukan menggunakan metode Object.Equals. Jika jenis elemen adalah jenis nonintrinsic (ditentukan pengguna), implementasi Equals
dari jenis tersebut digunakan.
Karena sebagian besar array akan memiliki batas nol yang lebih rendah, metode ini umumnya akan mengembalikan -1 ketika value
tidak ditemukan. Dalam kasus yang jarang terjadi bahwa batas bawah array sama dengan Int32.MinValue dan value
tidak ditemukan, metode ini mengembalikan Int32.MaxValue, yang System.Int32.MinValue - 1
.
Metode ini adalah operasi O(n
), di mana n
adalah jumlah elemen dari awal array
hingga startIndex
.
Dalam .NET Framework 2.0 dan versi yang lebih baru, metode ini menggunakan metode Equals dan CompareTo dari Array untuk menentukan apakah Object ditentukan oleh parameter value
ada. Dalam versi .NET Framework sebelumnya, penentuan ini dibuat dengan menggunakan metode Equals dan CompareTo dari value
Object itu sendiri.
Lihat juga
Berlaku untuk
LastIndexOf(Array, Object, Int32, Int32)
- Sumber:
- Array.cs
- Sumber:
- Array.cs
- Sumber:
- Array.cs
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array satu dimensi yang berisi jumlah elemen yang ditentukan dan berakhir pada indeks yang ditentukan.
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
Parameter
- value
- Object
Objek yang akan ditemukan di array
.
- startIndex
- Int32
Indeks awal pencarian mundur.
- count
- Int32
Jumlah elemen di bagian untuk dicari.
Mengembalikan
Indeks kemunculan terakhir value
dalam rentang elemen dalam array
yang berisi jumlah elemen yang ditentukan dalam count
dan berakhir pada startIndex
, jika ditemukan; jika tidak, batas bawah array dikurangi 1.
Pengecualian
array
null
.
startIndex
berada di luar rentang indeks yang valid untuk array
.
-atau-
count
kurang dari nol.
-atau-
startIndex
dan count
tidak menentukan bagian yang valid di array
.
array
multidirmani.
Contoh
Contoh kode berikut menunjukkan cara menentukan indeks kemunculan terakhir elemen tertentu dalam array. Perhatikan bahwa metode LastIndexOf adalah pencarian mundur; oleh karena itu, count
harus kurang dari atau sama dengan (startIndex
dikurangi batas bawah array ditambah 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.
Keterangan
Array satu dimensi dicari mundur mulai dari startIndex
dan berakhir pada startIndex
dikurangi count
ditambah 1, jika count
lebih besar dari 0.
Elemen dibandingkan dengan nilai yang ditentukan menggunakan metode Object.Equals. Jika jenis elemen adalah jenis nonintrinsic (ditentukan pengguna), implementasiEquals
dari jenis tersebut digunakan.
Karena sebagian besar array akan memiliki batas nol yang lebih rendah, metode ini umumnya akan mengembalikan -1 ketika value
tidak ditemukan. Dalam kasus yang jarang terjadi bahwa batas bawah array sama dengan Int32.MinValue dan value
tidak ditemukan, metode ini mengembalikan Int32.MaxValue, yang System.Int32.MinValue - 1
.
Metode ini adalah operasi O(n
), di mana n
count
.
Dalam .NET Framework 2.0 dan versi yang lebih baru, metode ini menggunakan metode Equals dan CompareTo dari Array untuk menentukan apakah Object ditentukan oleh parameter value
ada. Dalam versi .NET Framework sebelumnya, penentuan ini dibuat dengan menggunakan metode Equals dan CompareTo dari value
Object itu sendiri.
Lihat juga
Berlaku untuk
LastIndexOf<T>(T[], T)
- Sumber:
- Array.cs
- Sumber:
- Array.cs
- Sumber:
- Array.cs
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam seluruh Array.
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
Jenis parameter
- T
Jenis elemen array.
Parameter
- array
- T[]
Array berbasis nol satu dimensi untuk dicari.
- value
- T
Objek yang akan ditemukan di array
.
Mengembalikan
Indeks berbasis nol dari kemunculan terakhir value
dalam seluruh array
, jika ditemukan; jika tidak, -1.
Pengecualian
array
null
.
Contoh
Contoh kode berikut menunjukkan ketiga kelebihan beban umum metode LastIndexOf. Array string dibuat, dengan satu entri yang muncul dua kali, di lokasi indeks 0 dan lokasi indeks 5. Metode LastIndexOf<T>(T[], T) kelebihan beban mencari seluruh array dari akhir, dan menemukan kemunculan kedua string. Overload metode LastIndexOf<T>(T[], T, Int32) digunakan untuk mencari array mundur dimulai dengan lokasi indeks 3 dan melanjutkan ke awal array, dan menemukan kemunculan pertama string. Akhirnya, metode LastIndexOf<T>(T[], T, Int32, Int32) kelebihan beban digunakan untuk mencari rentang empat entri, dimulai di lokasi indeks 4 dan memperluas mundur (yaitu, ia mencari item di lokasi 4, 3, 2, dan 1); pencarian ini mengembalikan -1 karena tidak ada instans string pencarian dalam rentang tersebut.
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
Keterangan
Array dicari mundur mulai dari elemen terakhir dan berakhir pada elemen pertama.
Elemen dibandingkan dengan nilai yang ditentukan menggunakan metode Object.Equals. Jika jenis elemen adalah jenis nonintrinsic (ditentukan pengguna), implementasi Equals
dari jenis tersebut digunakan.
Metode ini adalah operasi O(n
), di mana n
adalah Lengtharray
.
Lihat juga
Berlaku untuk
LastIndexOf<T>(T[], T, Int32)
- Sumber:
- Array.cs
- Sumber:
- Array.cs
- Sumber:
- Array.cs
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array yang meluas dari elemen pertama ke indeks yang ditentukan.
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
Jenis parameter
- T
Jenis elemen array.
Parameter
- array
- T[]
Array berbasis nol satu dimensi untuk dicari.
- value
- T
Objek yang akan ditemukan di array
.
- startIndex
- Int32
Indeks awal berbasis nol dari pencarian mundur.
Mengembalikan
Indeks berbasis nol dari kemunculan terakhir value
dalam rentang elemen dalam array
yang meluas dari elemen pertama ke startIndex
, jika ditemukan; jika tidak, -1.
Pengecualian
array
null
.
startIndex
berada di luar rentang indeks yang valid untuk array
.
Contoh
Contoh kode berikut menunjukkan ketiga kelebihan beban umum metode LastIndexOf. Array string dibuat, dengan satu entri yang muncul dua kali, di lokasi indeks 0 dan lokasi indeks 5. Metode LastIndexOf<T>(T[], T) kelebihan beban mencari seluruh array dari akhir, dan menemukan kemunculan kedua string. Overload metode LastIndexOf<T>(T[], T, Int32) digunakan untuk mencari array mundur dimulai dengan lokasi indeks 3 dan melanjutkan ke awal array, dan menemukan kemunculan pertama string. Akhirnya, metode LastIndexOf<T>(T[], T, Int32, Int32) kelebihan beban digunakan untuk mencari rentang empat entri, dimulai di lokasi indeks 4 dan memperluas mundur (yaitu, ia mencari item di lokasi 4, 3, 2, dan 1); pencarian ini mengembalikan -1 karena tidak ada instans string pencarian dalam rentang tersebut.
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
Keterangan
Array dicari mundur mulai dari startIndex
dan berakhir pada elemen pertama.
Elemen dibandingkan dengan nilai yang ditentukan menggunakan metode Object.Equals. Jika jenis elemen adalah jenis nonintrinsic (ditentukan pengguna), implementasi Equals
dari jenis tersebut digunakan.
Metode ini adalah operasi O(n
), di mana n
adalah jumlah elemen dari awal array
hingga startIndex
.
Lihat juga
Berlaku untuk
LastIndexOf<T>(T[], T, Int32, Int32)
- Sumber:
- Array.cs
- Sumber:
- Array.cs
- Sumber:
- Array.cs
Mencari objek yang ditentukan dan mengembalikan indeks kemunculan terakhir dalam rentang elemen dalam Array yang berisi jumlah elemen yang ditentukan dan berakhir pada indeks yang ditentukan.
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
Jenis parameter
- T
Jenis elemen array.
Parameter
- array
- T[]
Array berbasis nol satu dimensi untuk dicari.
- value
- T
Objek yang akan ditemukan di array
.
- startIndex
- Int32
Indeks awal berbasis nol dari pencarian mundur.
- count
- Int32
Jumlah elemen di bagian untuk dicari.
Mengembalikan
Indeks berbasis nol dari kemunculan terakhir value
dalam rentang elemen dalam array
yang berisi jumlah elemen yang ditentukan dalam count
dan berakhir pada startIndex
, jika ditemukan; jika tidak, -1.
Pengecualian
array
null
.
startIndex
berada di luar rentang indeks yang valid untuk array
.
-atau-
count
kurang dari nol.
-atau-
startIndex
dan count
tidak menentukan bagian yang valid di array
.
Contoh
Contoh kode berikut menunjukkan ketiga kelebihan beban umum metode LastIndexOf. Array string dibuat, dengan satu entri yang muncul dua kali, di lokasi indeks 0 dan lokasi indeks 5. Metode LastIndexOf<T>(T[], T) kelebihan beban mencari seluruh array dari akhir, dan menemukan kemunculan kedua string. Overload metode LastIndexOf<T>(T[], T, Int32) digunakan untuk mencari array mundur dimulai dengan lokasi indeks 3 dan melanjutkan ke awal array, dan menemukan kemunculan pertama string. Akhirnya, metode LastIndexOf<T>(T[], T, Int32, Int32) kelebihan beban digunakan untuk mencari rentang empat entri, dimulai di lokasi indeks 4 dan memperluas mundur (yaitu, ia mencari item di lokasi 4, 3, 2, dan 1); pencarian ini mengembalikan -1 karena tidak ada instans string pencarian dalam rentang tersebut.
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
Keterangan
Array dicari mundur mulai dari startIndex
dan berakhir pada startIndex
dikurangi count
ditambah 1, jika count
lebih besar dari 0.
Elemen dibandingkan dengan nilai yang ditentukan menggunakan metode Object.Equals. Jika jenis elemen adalah jenis nonintrinsic (ditentukan pengguna), implementasi Equals
dari jenis tersebut digunakan.
Metode ini adalah operasi O(n
), di mana n
count
.