Array.LastIndexOf方法用于查找一维Array或部分Array中值最后一次出现的索引。它从数组的最后一个元素开始搜索。它返回包含指定值的元素的索引。此方法的重载列表中有6种方法,如下所示:
- LastIndexOf(Array,Object)
- LastIndexOf(Array,Object,Int32)
- LastIndexOf(Array,Object,Int32,Int32)
- LastIndexOf
(T [],T) - LastIndexOf
(T [],T,Int32) - LastIndexOf
(T [],T,Int32,Int32)
在这里,我们将讨论最后三种方法。
LastIndexOf (T [],T)方法
此方法搜索指定的对象,并返回整个一维数组中最后一次出现的索引。
Syntax: public static int LastIndexOf
Here, T is the type of the elements of the array.
Parameters:
arr: It is the one-dimensional array.
value: It is the object to locate in array.
返回值:如果找到对象,则返回整个数组中最后一次出现的值的从零开始的索引,否则返回-1。
异常:如果数组为null,则此方法将提供ArgumentNullException。
例子:
// C# program to demonstrate the
// Array.LastIndexOf(T[], T)
using System;
class GFG {
// Main Method
public static void Main()
{
// array elements
string[] arr = { "ABC",
"EFG",
"GHI",
"LMO",
"STW",
"XYZ",
"QRS" };
Console.WriteLine("Original Array");
foreach(string g in arr)
{
Console.WriteLine("Original array" + g);
}
// here the object is STW
Console.WriteLine("\nThe position of STW is " +
Array.LastIndexOf(arr, "STW"));
}
}
Original Array
Original arrayABC
Original arrayEFG
Original arrayGHI
Original arrayLMO
Original arraySTW
Original arrayXYZ
Original arrayQRS
The position of STW is 4
LastIndexOf (T [],T,Int32)方法
此方法搜索指定的对象,并返回从第一个元素到指定索引的Array元素范围内最后一次出现的索引。
Syntax: public static int LastIndexOf
Here, T is the type of the elements of the array.
Parameters:
arr: It is the one-dimensional array.
value: It is the object to locate in array
start: It is the zero-based starting index of the backward search.
例外情况:
- ArgumentNullException:如果数组为null。
- ArgumentOutOfRangeException:如果“开始”超出数组的有效索引范围。
例子:
// C# program to demonstrate the
// Array.LastIndexOf(T[], T, Int32)
using System;
class GFG {
// Main Method
public static void Main()
{
// array elements
string[] arr = { "ABC",
"EFG",
"GHI",
"LMO",
"STW",
"XYZ",
"QRS" };
Console.WriteLine("Original Array");
foreach(string g in arr)
{
Console.WriteLine(g);
}
// Here the object is STW
// the search starts from index 5
Console.WriteLine("\nthe position of STW is " +
Array.LastIndexOf(arr, "STW", 5));
}
}
Original Array
ABC
EFG
GHI
LMO
STW
XYZ
QRS
the position of STW is 4
LastIndexOf (T [],T,Int32,Int32)方法
此方法搜索指定的对象,并返回包含指定数量的元素并在指定索引处结束的Array元素范围内最后一次出现的索引。
Syntax: public static int LastIndexOf
Parameters:
arr: It is the one-dimensional array.
value: It is the object to locate in array.
start: It is the zero-based starting index of the backward search.
count: It is the number of elements in the section to search.
返回值:该方法将返回数组中元素的范围内最后一次出现的值的从零开始的索引,该数组包含包含在count中指定的元素数,如果找到则在开始时结束;否则,返回0。否则为-1。
例外情况:
- ArgumentNullException:如果数组为null。
- ArgumentOutOfRangeException:如果开始位置超出数组的有效索引范围或开始位置和计数,请不要在数组中指定有效部分。
例子:
// C# program to demonstrate the
// Array.LastIndexOf(T[], T,
// Int32, Int32)
using System;
class GFG {
// Main Method
public static void Main()
{
// array elements
string[] arr = { "ABC",
"EFG",
"GHI",
"LMO",
"STW",
"XYZ",
"QRS" };
Console.WriteLine("Original Array");
foreach(string g in arr)
{
Console.WriteLine(g);
}
// here the object is STW
// the search starts from index 5
// the search happens bacward from
// index 5 to another 2 index
Console.WriteLine("\nthe position of STW is " +
Array.LastIndexOf(arr, "STW", 5, 2));
}
}
Original Array
ABC
EFG
GHI
LMO
STW
XYZ
QRS
the position of STW is 4
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.array.lastindexof?view=netframework-4.7.2