本文整理匯總了C#中System.Array.IndexOf方法的典型用法代碼示例。如果您正苦於以下問題:C# Array.IndexOf方法的具體用法?C# Array.IndexOf怎麽用?C# Array.IndexOf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Array
的用法示例。
在下文中一共展示了Array.IndexOf方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: for
// Create a string array with 3 elements having the same value.
String[] strings = { "the", "quick", "brown", "fox", "jumps",
"over", "the", "lazy", "dog", "in", "the",
"barn" };
// Display the elements of the array.
Console.WriteLine("The array contains the following values:");
for (int i = strings.GetLowerBound(0); i <= strings.GetUpperBound(0); i++)
Console.WriteLine(" [{0,2}]: {1}", i, strings[i]);
// Search for the first occurrence of the duplicated value.
String searchString = "the";
int index = Array.IndexOf(strings, searchString);
Console.WriteLine("The first occurrence of \"{0}\" is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in the last section of the array.
index = Array.IndexOf(strings, searchString, 4);
Console.WriteLine("The first occurrence of \"{0}\" between index 4 and the end is at index {1}.",
searchString, index);
// Search for the first occurrence of the duplicated value in a section of the array.
int position = index + 1;
index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound(0) - position + 1);
Console.WriteLine("The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.",
searchString, position, strings.GetUpperBound(0), index);
輸出:
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 first occurrence of "the" is at index 0. The first occurrence of "the" between index 4 and the end is at index 6. The first occurrence of "the" between index 7 and index 11 is at index 10.
示例2: foreach
string[] dinosaurs = { "Tyrannosaurus",
"Amargasaurus",
"Mamenchisaurus",
"Brachiosaurus",
"Deinonychus",
"Tyrannosaurus",
"Compsognathus" };
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus"));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 3));
Console.WriteLine(
"\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): {0}",
Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2));
輸出:
Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf(dinosaurs, "Tyrannosaurus"): 0 Array.IndexOf(dinosaurs, "Tyrannosaurus", 3): 5 Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2): -1
示例3: Main
//引入命名空間
using System;
class MainClass
{
public static void Main()
{
int[] intArray = {1, 2, 1, 3};
Console.WriteLine("intArray:");
for (int i = 0; i < intArray.Length; i++)
{
Console.WriteLine("intArray[" + i + "] = " +
intArray[i]);
}
int index = Array.IndexOf(intArray, 1);
Console.WriteLine("Array.IndexOf(intArray, 1) = " + index);
index = Array.LastIndexOf(intArray, 1);
Console.WriteLine("Array.LastIndexOf(intArray, 1) = " + index);
}
}