当前位置: 首页>>代码示例>>C#>>正文


C# Array.IndexOf方法代码示例

本文整理汇总了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);
开发者ID:.NET开发者,项目名称:System,代码行数:26,代码来源:Array.IndexOf

输出:

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));
开发者ID:.NET开发者,项目名称:System,代码行数:25,代码来源:Array.IndexOf

输出:

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);
  }

}
开发者ID:C#程序员,项目名称:System,代码行数:24,代码来源:Array.IndexOf


注:本文中的System.Array.IndexOf方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。