C#中的Array.LastIndexOf(T [],T)方法
C#中的Array.LastIndexOf()方法用于搜索指定的对象,并返回整个Array中最后一次出现的索引。
语法
以下是语法-
public static int LastIndexOf<T> (T[] array, T val);
上面的参数arr是要搜索的一维,从零开始的数组,而Val是要在数组中定位的对象。
示例
现在让我们看一个实现Array.LastIndexOf()方法的示例-
using System;
public class Demo{
public static void Main(){
Console.WriteLine("Array elements...");
string[] arr = { "car", "bike", "truck", "bus"};
for (int i = 0; i < arr.Length; i++){
Console.Write("{0} ", arr[i]);
}
Console.WriteLine();
int lastIndex = Array.LastIndexOf(arr, "bus");
Console.WriteLine("Last Ocurrence of element bus is at index = "+lastIndex);
}
}输出结果
这将产生以下输出-
Array elements... car bike truck bus Last Ocurrence of element bus is at index = 3
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话