Array.LastIndexOf 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
LastIndexOf(Array, Object) |
在整个一维 Array 中搜索指定的对象,并返回最后一个匹配项的索引。 |
LastIndexOf(Array, Object, Int32) |
搜索指定的对象,并返回一维 Array 中从第一个元素到指定索引的元素范围内最后一个匹配项的索引。 |
LastIndexOf(Array, Object, Int32, Int32) |
搜索指定的对象并返回一维 Array 中包含指定数目元素且在指定索引处结尾的元素范围内的最后一个匹配项的索引。 |
LastIndexOf<T>(T[], T) |
搜索指定的对象,并返回整个 Array 中最后一个匹配项的索引。 |
LastIndexOf<T>(T[], T, Int32) |
搜索指定的对象并返回 Array 中从第一个元素到指定索引的元素范围内最后一个匹配项的索引。 |
LastIndexOf<T>(T[], T, Int32, Int32) |
搜索指定的对象,并返回 Array 中包含指定数目元素并在指定索引处结尾的元素范围内的最后一个匹配项的索引。 |
LastIndexOf(Array, Object)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
在整个一维 Array 中搜索指定的对象,并返回最后一个匹配项的索引。
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
参数
- value
- Object
要在 array
中查找的对象。
返回
如果在整个 array
中找到 value
的最后一个匹配项的索引,则为该索引;否则为该数组的下限减 1。
例外
array
为 null
。
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.
注解
从最后一个元素开始,在第一个元素结束,向后搜索一维 Array 。
使用 Object.Equals 方法将元素与指定的值进行比较。 如果元素类型是非内部 (用户定义的) 类型,则 Equals
使用该类型的实现。
由于大多数数组的下限为零,因此当找不到 时 value
,此方法通常会返回 -1。 在极少数情况下,数组的下限等于 Int32.MinValue 且 value
未找到,此方法返回 Int32.MaxValue,即 System.Int32.MinValue - 1
。
此方法是 O (n
) 操作,其中 n
是 Length 的 array
。
在 .NET Framework 2.0 及更高版本中,此方法使用 Equals 的 Array 和 CompareTo 方法来确定 参数指定的 value
是否存在Object。 在早期版本的 .NET Framework 中,此确定是使用 Equals 本身的 和 CompareTo 方法做出的value
Object。
CompareToitem
集合中对象上的 参数的方法。
另请参阅
适用于
LastIndexOf(Array, Object, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索指定的对象,并返回一维 Array 中从第一个元素到指定索引的元素范围内最后一个匹配项的索引。
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
参数
- value
- Object
要在 array
中查找的对象。
- startIndex
- Int32
向后搜索的起始索引。
返回
如果在 array
中从第一个元素到 startIndex
的元素范围内找到了 value
的最后一个匹配项的索引,则为该索引;否则为该数组的下限减 1。
例外
array
为 null
。
startIndex
超出了 array
的有效索引范围。
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.
注解
从第一个元素开始startIndex
向后搜索一维Array,并在第一个元素处结束。
使用 Object.Equals 方法将元素与指定的值进行比较。 如果元素类型是非内部 (用户定义的) 类型,则 Equals
使用该类型的实现。
由于大多数数组的下限为零,因此当找不到 时 value
,此方法通常会返回 -1。 在极少数情况下,数组的下限等于 Int32.MinValue 且 value
未找到,此方法返回 Int32.MaxValue,即 System.Int32.MinValue - 1
。
此方法是 O (n
) 操作,其中 n
是从 的开头到 startIndex
的array
元素数。
在 .NET Framework 2.0 及更高版本中,此方法使用 Equals 的 Array 和 CompareTo 方法来确定 参数指定的 value
是否存在Object。 在早期版本的 .NET Framework 中,此确定是使用 Equals 本身的 和 CompareTo 方法做出的value
Object。
另请参阅
适用于
LastIndexOf(Array, Object, Int32, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索指定的对象并返回一维 Array 中包含指定数目元素且在指定索引处结尾的元素范围内的最后一个匹配项的索引。
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
参数
- value
- Object
要在 array
中查找的对象。
- startIndex
- Int32
向后搜索的起始索引。
- count
- Int32
要搜索的部分中的元素数。
返回
如果在包含 count
中指定的元素数并且在 startIndex
结尾的 array
中的元素范围内找到了 value
的最后一个匹配项的索引,则为该索引;否则为该数组的下限减 1。
例外
array
为 null
。
startIndex
超出了 array
的有效索引范围。
或
count
小于零。
或
startIndex
和 count
未在 array
中指定有效部分。
array
是多维的。
示例
下面的代码示例演示如何确定数组中指定元素的最后一个匹配项的索引。 请注意, LastIndexOf 方法是向后搜索;因此, count
必须小于或等于 (startIndex
减去数组的下限加 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.
注解
如果 大于 0,则向后搜索一维Array,从startIndex
减去count
加 1 结束startIndex
。count
使用 Object.Equals 方法将元素与指定的值进行比较。 如果元素类型是非内部 (用户定义的) 类型,则Equals
使用该类型的实现。
由于大多数数组的下限为零,因此当找不到 时 value
,此方法通常会返回 -1。 在极少数情况下,数组的下限等于 Int32.MinValue 且 value
未找到,此方法返回 Int32.MaxValue,即 System.Int32.MinValue - 1
。
此方法是 O (n
) 操作,其中 n
为 count
。
在 .NET Framework 2.0 及更高版本中,此方法使用 Equals 的 Array 和 CompareTo 方法来确定 参数指定的 value
是否存在Object。 在早期版本的 .NET Framework 中,此确定是使用 Equals 本身的 和 CompareTo 方法做出的value
Object。
另请参阅
适用于
LastIndexOf<T>(T[], T)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索指定的对象,并返回整个 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
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的从零开始的一维 Array。
- value
- T
要在 array
中查找的对象。
返回
如果在整个 array
中找到 value
的最后一个匹配项从零开始的索引,则为该索引;否则为 -1。
例外
array
为 null
。
示例
下面的代码示例演示 方法的所有三个 LastIndexOf 泛型重载。 创建一个字符串数组,其中一个条目在索引位置 0 和索引位置 5 出现两次。 方法 LastIndexOf<T>(T[], T) 重载从末尾搜索整个数组,并查找字符串的第二个匹配项。 方法 LastIndexOf<T>(T[], T, Int32) 重载用于向后搜索从索引位置 3 开始的数组,一再搜索到数组的开头,并查找字符串的第一个匹配项。 最后, LastIndexOf<T>(T[], T, Int32, Int32) 方法重载用于搜索四个条目的范围,从索引位置 4 开始,向后扩展 (即搜索位置 4、3、2 和 1) 位置的项;此搜索返回 -1,因为该范围内没有搜索字符串的实例。
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
注解
从 Array 最后一个元素开始,在第一个元素结束,向后搜索 。
使用 Object.Equals 方法将元素与指定的值进行比较。 如果元素类型是非内部 (用户定义的) 类型,则 Equals
使用该类型的实现。
此方法是 O (n
) 操作,其中 n
是 Length 的 array
。
另请参阅
适用于
LastIndexOf<T>(T[], T, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索指定的对象并返回 Array 中从第一个元素到指定索引的元素范围内最后一个匹配项的索引。
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
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的从零开始的一维 Array。
- value
- T
要在 array
中查找的对象。
- startIndex
- Int32
向后搜索的从零开始的起始索引。
返回
如果在 array
中从第一个元素到 startIndex
的元素范围中找到了该 value
最后一个匹配项从零开始的索引,则为该索引;否则为 -1。
例外
array
为 null
。
startIndex
超出了 array
的有效索引范围。
示例
下面的代码示例演示 方法的所有三个 LastIndexOf 泛型重载。 创建一个字符串数组,其中一个条目在索引位置 0 和索引位置 5 出现两次。 方法 LastIndexOf<T>(T[], T) 重载从末尾搜索整个数组,并查找字符串的第二个匹配项。 方法 LastIndexOf<T>(T[], T, Int32) 重载用于向后搜索从索引位置 3 开始的数组,一再搜索到数组的开头,并查找字符串的第一个匹配项。 最后, LastIndexOf<T>(T[], T, Int32, Int32) 方法重载用于搜索四个条目的范围,从索引位置 4 开始,向后扩展 (即搜索位置 4、3、2 和 1) 位置的项;此搜索返回 -1,因为该范围内没有搜索字符串的实例。
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
注解
Array从 第一个元素开始startIndex
向后搜索,并在第一个元素处结束。
使用 Object.Equals 方法将元素与指定的值进行比较。 如果元素类型是非内部 (用户定义的) 类型,则 Equals
使用该类型的实现。
此方法是 O (n
) 操作,其中 n
是从 的开头到 startIndex
的array
元素数。
另请参阅
适用于
LastIndexOf<T>(T[], T, Int32, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
搜索指定的对象,并返回 Array 中包含指定数目元素并在指定索引处结尾的元素范围内的最后一个匹配项的索引。
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
类型参数
- T
数组元素的类型。
参数
- array
- T[]
要搜索的从零开始的一维 Array。
- value
- T
要在 array
中查找的对象。
- startIndex
- Int32
向后搜索的从零开始的起始索引。
- count
- Int32
要搜索的部分中的元素数。
返回
如果在 startIndex
中找到了包含 count
中指定元素数并且到 array
结尾的元素范围内的 value
的最后一个匹配项的从零开始的索引,则为该索引;否则为 -1。
例外
array
为 null
。
startIndex
超出了 array
的有效索引范围。
或
count
小于零。
或
startIndex
和 count
未在 array
中指定有效部分。
示例
下面的代码示例演示 方法的所有三个 LastIndexOf 泛型重载。 创建一个字符串数组,其中一个条目在索引位置 0 和索引位置 5 出现两次。 方法 LastIndexOf<T>(T[], T) 重载从末尾搜索整个数组,并查找字符串的第二个匹配项。 方法 LastIndexOf<T>(T[], T, Int32) 重载用于向后搜索从索引位置 3 开始的数组,一再搜索到数组的开头,并查找字符串的第一个匹配项。 最后, LastIndexOf<T>(T[], T, Int32, Int32) 方法重载用于搜索四个条目的范围,从索引位置 4 开始,向后扩展 (即搜索位置 4、3、2 和 1) 位置的项;此搜索返回 -1,因为该范围内没有搜索字符串的实例。
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
注解
Array如果 count
大于 0,则向后搜索 ,从startIndex
减去count
加 1 结束startIndex
。
使用 Object.Equals 方法将元素与指定的值进行比较。 如果元素类型是非内部 (用户定义的) 类型,则 Equals
使用该类型的实现。
此方法是 O (n
) 操作,其中 n
为 count
。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈