C# 数组:如何查找指定元素的索引
在 C# 中,要找到一个指定元素的索引,可以使用 Array 类的 IndexOf 方法。
例如,如果要查找字符串数组中的特定字符串 'apple' 的索引,可以使用以下代码:
string[] fruits = { 'banana', 'apple', 'orange', 'grape' };
int index = Array.IndexOf(fruits, 'apple');
Console.WriteLine('Index of 'apple': ' + index);
这将输出 'Index of 'apple': 1',因为第二个元素是 'apple',它的索引是 1。如果数组中没有找到指定的元素,IndexOf 方法将返回 -1。
原文地址: https://www.cveoy.top/t/topic/oK94 著作权归作者所有。请勿转载和采集!