C#中的Array.LastIndexOf()方法用于搜索指定对象并返回整个一维Array中最后一次出现的索引。
用法
public static int LastIndexOf (Array arr, object val);
上面,arr 是要搜索的一维数组,而 val 是要在 arr 中定位的对象。
示例
using System;
public class Demo {
public static void Main() {
string[] strArr = {"John", "Tim", "Fedric", "Gary", "Harry", "Damien", "David", "Harry"};
Array.Sort(strArr);
Console.WriteLine("Array elements...");
foreach(string s in strArr) {
Console.WriteLine(s);
}
Console.Write("Element Gary is at index = " + Array.BinarySearch(strArr, "Gary"));
Console.Write("\nElement Tom is at index = " + Array.BinarySearch(strArr, "Tom"));
Console.Write("\nLast index of element Harry = " + Array.LastIndexOf(strArr, "Harry"));
}
}
输出
Array elements... Damien David Fedric Gary Harry Harry John Tim Element Gary is at index = 3 Element Tom is at index = -9 Last index of element Harry = 5
示例
using System;
public class Demo {
public static void Main() {
int[] intArr = {5, 10, 15, 20, 15, 25, 30};
Array.Sort(intArr);
Console.WriteLine("Array elements...");
foreach(int i in intArr) {
Console.WriteLine(i);
}
Console.Write("Element 25 is at index = " + Array.BinarySearch(intArr, 20));
Console.Write("\nLast index of element 15 = " + Array.LastIndexOf(intArr, 15));
Console.Write("\nLast index of element 50 = " + Array.LastIndexOf(intArr, 50));
}
}
输出
Array elements... 5 10 15 15 20 25 30 Element 25 is at index = 4 Last index of element 15 = 3 Last index of element 50 = -1
相关用法
- C# Array.FindLast()用法及代码示例
- C# Array.BinarySearch(Array, Int32, Int32, Object, IComparer)用法及代码示例
- C# Array.BinarySearch(Array, Object, IComparer)用法及代码示例
- C# Array.AsReadOnly(T[])用法及代码示例
- C# Array.GetValue()方法用法及代码示例
- C# Array.FindAll()用法及代码示例
- C# Array.Find()用法及代码示例
- C# Array.TrueForAll()用法及代码示例
- C# Array.ConstrainedCopy()用法及代码示例
- C# Array.Clear()用法及代码示例
- C# Array.GetEnumerator用法及代码示例
- C# Array.GetValue()函数用法及代码示例
- C# ArrayList.InsertRange()用法及代码示例
- C# Decimal.FromOACurrency()用法及代码示例
- C# Int32.CompareTo用法及代码示例
- C# UInt64.ToString()用法及代码示例
- C# Type.GetTypeHandle()用法及代码示例
- C# Uri.IsBaseOf()用法及代码示例
- C# String.ToUpperInvariant用法及代码示例
- C# File.Copy(String, String, Boolean)用法及代码示例
注:本文由纯净天空筛选整理自AmitDiwan大神的英文原创作品 Array.LastIndexOf() Method in C#。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。