Array.IndexOf Array.IndexOf Array.IndexOf Array.IndexOf Method

定义

在一个一维数组或该数组的一系列元素中搜索指定对象,并返回其首个匹配项的索引。Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array.

重载

IndexOf(Array, Object) IndexOf(Array, Object) IndexOf(Array, Object)

在一个一维数组中搜索指定对象,并返回其首个匹配项的索引。Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

IndexOf(Array, Object, Int32) IndexOf(Array, Object, Int32) IndexOf(Array, Object, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. 范围从指定索引到该数组结尾。The range extends from a specified index to the end of the array.

IndexOf(Array, Object, Int32, Int32) IndexOf(Array, Object, Int32, Int32) IndexOf(Array, Object, Int32, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of ifs first occurrence. 该元素系列的范围从指定数量的元素的指定索引开始。The range extends from a specified index for a specified number of elements.

IndexOf<T>(T[], T, Int32, Int32) IndexOf<T>(T[], T, Int32, Int32) IndexOf<T>(T[], T, Int32, Int32) IndexOf<T>(T[], T, Int32, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. 该元素系列的范围从指定数量的元素的指定索引开始。The range extends from a specified index for a specified number of elements.

IndexOf<T>(T[], T) IndexOf<T>(T[], T) IndexOf<T>(T[], T) IndexOf<T>(T[], T)

在一个一维数组中搜索指定对象,并返回其首个匹配项的索引。Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

IndexOf<T>(T[], T, Int32) IndexOf<T>(T[], T, Int32) IndexOf<T>(T[], T, Int32) IndexOf<T>(T[], T, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one dimensional array, and returns the index of its first occurrence. 范围从指定索引到该数组结尾。The range extends from a specified index to the end of the array.

IndexOf(Array, Object) IndexOf(Array, Object) IndexOf(Array, Object)

在一个一维数组中搜索指定对象,并返回其首个匹配项的索引。Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

public:
 static int IndexOf(Array ^ array, System::Object ^ value);
public static int IndexOf (Array array, object value);
static member IndexOf : Array * obj -> int

参数

array
Array Array Array Array

要搜索的一维数组。The one-dimensional array to search.

value
Object Object Object Object

要在 array 中查找的对象。The object to locate in array.

返回

如果找到,则为 arrayvalue 的第一个匹配项的索引;否则为该数组的下限减 1。The index of the first occurrence of value in array, if found; otherwise, the lower bound of the array minus 1.

异常

array 是多维的。array is multidimensional.

示例

该示例调用IndexOf方法的以下三种重载来查找字符串数组中字符串的索引:The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:

using namespace System;

void main()
{
   // Create a string array with 3 elements having the same value.
   array<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 example displays the following output:
//    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.
using System;

public class Example
{
    public static void Main()
    {
       // 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 example displays the following output:
//    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.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }

      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      Next

      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = 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.
       Dim position As Integer = 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)
    End Sub
End Module
' The example displays the following output:
'    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.

注解

此方法搜索一维数组value的所有元素。This method searches all the elements of a one-dimensional array for value. 若要确定valuearray是否存在, 方法会通过调用每个元素的Equals方法来执行相等比较, 直到找到匹配项为止。To determine whether value exists in array, the method performs an equality comparison by calling each element's Equals method until it finds a match. 这意味着, 如果元素重写Object.Equals(Object)方法, 则将调用该重写。This means that if the element overrides the Object.Equals(Object) method, that override is called.

由于大多数数组的下限均为零, 因此如果value找不到, 则此方法通常返回-1。Because most arrays have a lower bound of zero, this method generally returns -1 ifvalue isn't found. 如果数组的下限等于Int32.MinValue(0x80000000), 并且value找不到, 则此方法返回Int32.MaxValue (0x7fffffff)。In the rare case that the lower bound of the array is equal to Int32.MinValue(0x80000000) and value isn't found, this method returns Int32.MaxValue (0x7FFFFFFF).

此方法为 On() 运算, 其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

另请参阅

IndexOf(Array, Object, Int32) IndexOf(Array, Object, Int32) IndexOf(Array, Object, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. 范围从指定索引到该数组结尾。The range extends from a specified index to the end of the array.

public:
 static int IndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int IndexOf (Array array, object value, int startIndex);
static member IndexOf : Array * obj * int -> int

参数

array
Array Array Array Array

要搜索的一维数组。The one-dimensional array to search.

value
Object Object Object Object

要在 array 中查找的对象。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

搜索的起始索引。The starting index of the search. 空数组中 0(零)为有效值。0 (zero) is valid in an empty array.

返回

如果在从 startIndex 到最后一个元素的 array 中的元素范围中找到了 value 的第一个匹配项的索引,则为该索引;否则为数组的下限减 1。The index of the first occurrence of value, if it's found, within the range of elements in array that extends from startIndex to the last element; otherwise, the lower bound of the array minus 1.

异常

startIndex 超出了 array 的有效索引范围。startIndex is outside the range of valid indexes for array.

array 是多维的。array is multidimensional.

示例

该示例调用IndexOf方法的以下三种重载来查找字符串数组中字符串的索引:The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:

using namespace System;

void main()
{
   // Create a string array with 3 elements having the same value.
   array<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 example displays the following output:
//    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.
using System;

public class Example
{
    public static void Main()
    {
       // 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 example displays the following output:
//    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.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }

      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      Next

      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = 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.
       Dim position As Integer = 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)
    End Sub
End Module
' The example displays the following output:
'    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.

注解

此方法搜索从索引startIndex处的元素到最后一个元素的一维数组。This method searches a one-dimensional array from the element at index startIndex to the last element. 若要确定valuearray是否存在, 方法会通过调用每个元素的Equals方法来执行相等比较, 直到找到匹配项为止。To determine whether value exists in array, the method performs an equality comparison by calling the Equals method of every element until it finds a match. 这意味着, 如果元素重写Object.Equals(Object)方法, 则将调用该重写。This means that if the element overrides the Object.Equals(Object) method, that override is called.

由于大多数数组的下限均为零, 因此如果value找不到, 则此方法通常返回-1。Because most arrays have a lower bound of zero, this method generally returns -1 if value isn't found. 如果数组的下限等于Int32.MinValue(0x80000000), 并且value找不到, 则此方法返回Int32.MaxValue (0x7fffffff)。In the rare case that the lower bound of the array is equal to Int32.MinValue(0x80000000) and value isn't found, this method returns Int32.MaxValue (0x7FFFFFFF).

如果startIndex 等于Array.Length, 则此方法返回-1。If startIndex equals Array.Length,the method returns -1. 如果startIndex 大于ArgumentOutOfRangeException, 则方法将引发。 Array.LengthIf startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

此方法为 On() 运算, 其中narraystartIndex到末尾的元素数。This method is an O(n) operation, where n is the number of elements from startIndex to the end of array.

另请参阅

IndexOf(Array, Object, Int32, Int32) IndexOf(Array, Object, Int32, Int32) IndexOf(Array, Object, Int32, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of ifs first occurrence. 该元素系列的范围从指定数量的元素的指定索引开始。The range extends from a specified index for a specified number of elements.

public:
 static int IndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int IndexOf (Array array, object value, int startIndex, int count);
static member IndexOf : Array * obj * int * int -> int

参数

array
Array Array Array Array

要搜索的一维数组。The one-dimensional array to search.

value
Object Object Object Object

要在 array 中查找的对象。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

搜索的起始索引。The starting index of the search. 空数组中 0(零)为有效值。0 (zero) is valid in an empty array.

count
Int32 Int32 Int32 Int32

要搜索的元素数。The number of elements to search.

返回

如果在从索引 startIndexstartIndex + count - 1 的 array 中找到了 value 的第一个匹配项的索引,则为该索引;否则为该数组的下限减 1。The index of the first occurrence of value, if it's found in the array from index startIndex to startIndex + count - 1; otherwise, the lower bound of the array minus 1.

异常

startIndex 超出了 array 的有效索引范围。startIndex is outside the range of valid indexes for array.

-or- count 小于零。count is less than zero.

- 或 --or- startIndexcount 未在 array 中指定有效部分。startIndex and count do not specify a valid section in array.

array 是多维的。array is multidimensional.

示例

该示例调用IndexOf方法的以下三种重载来查找字符串数组中字符串的索引:The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:

  • IndexOf(Array, Object), 用于确定字符串数组中字符串 "the" 的第一个匹配项。IndexOf(Array, Object), to determine the first occurrence of the string "the" in a string array.

  • IndexOf(Array, Object, Int32), 用于确定第四个字符串中的第一个匹配项到字符串数组的最后一个元素。IndexOf(Array, Object, Int32), to determine the first occurrence of the string "the" in the fourth to the last elements of a string array.

  • IndexOf(Array, Object, Int32, Int32), 用于确定字符串数组中字符串 "the" 的第一个匹配项, 该字符串是从最后一个成功匹配的元素到数组末尾的下一个元素。IndexOf(Array, Object, Int32, Int32), to determine the first occurrence of the string "the" in a string array from the element that follows the last successful match to the end of the array. 若要确定count参数的值, 它将从起始索引中减去数组的上限并添加一个。To determine the value of the count argument, it subtracts the upper bound of the array from the starting index and adds one.

using namespace System;

void main()
{
   // Create a string array with 3 elements having the same value.
   array<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 example displays the following output:
//    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.
using System;

public class Example
{
    public static void Main()
    {
       // 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 example displays the following output:
//    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.
Public Module Example
   Public Sub Main()
      ' Create a string array with 3 elements having the same value.
      Dim strings() As String = { "the", "quick", "brown", "fox",
                                  "jumps", "over", "the", "lazy",
                                  "dog", "in", "the", "barn" }

      ' Display the values of the array.
      Console.WriteLine("The array contains the following values:")
      For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
         Console.WriteLine("   [{0,2}]: {1}", i, strings(i))
      Next

      ' Search for the first occurrence of the duplicated value.
      Dim searchString As String = "the"
      Dim index As Integer = 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.
       Dim position As Integer = 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)
    End Sub
End Module
' The example displays the following output:
'    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.

注解

如果startIndex startIndex count大于 0, 则此方法将从一维数组的元素搜索到减号1。 countThis method searches the elements of a one-dimensional array from startIndex to startIndex plus count minus 1, if count is greater than 0. 若要确定valuearray是否存在, 方法会通过调用每个元素的Equals方法来执行相等比较, 直到找到匹配项为止。To determine whether value exists in array, the method performs an equality comparison by calling the Equals method of every element until it finds a match. 这意味着, 如果元素重写Object.Equals方法, 则将调用该重写。This means that if the element overrides the Object.Equals method, that override is called.

由于大多数数组的下限均为零, 因此当找不到时value , 此方法通常返回-1。Because most arrays have a lower bound of zero, this method generally returns -1 when value isn't found. 如果数组的下限等于Int32.MinValue (0x80000000), 并且value找不到, 则此方法返回Int32.MaxValue (0x7fffffff)。In the rare case that the lower bound of the array is equal to Int32.MinValue (0x80000000) and value isn't found, this method returns Int32.MaxValue (0x7FFFFFFF).

如果startindex 等于Array.Length, 则此方法返回-1。If startindex equals Array.Length, the method returns -1. 如果startIndex 大于ArgumentOutOfRangeException, 则方法将引发。 Array.LengthIf startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

此方法是 O (n) 运算, 其中ncountThis method is an O(n) operation, where n is count.

另请参阅

IndexOf<T>(T[], T, Int32, Int32) IndexOf<T>(T[], T, Int32, Int32) IndexOf<T>(T[], T, Int32, Int32) IndexOf<T>(T[], T, Int32, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. 该元素系列的范围从指定数量的元素的指定索引开始。The range extends from a specified index for a specified number of elements.

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int IndexOf<T> (T[] array, T value, int startIndex, int count);
static member IndexOf : 'T[] * 'T * int * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer, count As Integer) As Integer

类型参数

T

数组元素的类型。The type of the elements of the array.

参数

array
T[]

要搜索的从零开始的一维数组。The one-dimensional, zero-based array to search.

value
T T T T

要在 array 中查找的对象。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

从零开始的搜索的起始索引。The zero-based starting index of the search. 空数组中 0(零)为有效值。0 (zero) is valid in an empty array.

count
Int32 Int32 Int32 Int32

要搜索的部分中的元素数。The number of elements in the section to search.

返回

如果在从 count 开始并且包含 startIndex 中指定元素数量的 array 元素范围中找到了 value 第一个匹配项从零开始的索引,则为该索引;否则为 -1。The zero-based index of the first occurrence of value within the range of elements in array that starts at startIndex and contains the number of elements specified in count, if found; otherwise, -1.

异常

startIndex 超出了 array 的有效索引范围。startIndex is outside the range of valid indexes for array.

- 或 --or- count 小于零。count is less than zero.

- 或 --or- startIndexcount 未在 array 中指定有效部分。startIndex and count do not specify a valid section in array.

示例

下面的示例演示IndexOf方法的所有三个泛型重载。The following example demonstrates all three generic overloads of the IndexOf method. 创建一个字符串数组, 其中有一个条目出现在索引位置0和索引位置5。An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. IndexOf<T>(T[], T)方法重载从开始处搜索数组, 并查找字符串的第一个匹配项。The IndexOf<T>(T[], T) method overload searches the array from the beginning, and finds the first occurrence of the string. IndexOf<T>(T[], T, Int32)方法重载用于从索引位置3开始搜索数组, 并继续到数组末尾, 并查找字符串的第二个匹配项。The IndexOf<T>(T[], T, Int32) method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. 最后, IndexOf<T>(T[], T, Int32, Int32)使用方法重载搜索两个条目的范围 (从第二个索引位置开始); 它返回-1, 因为该范围中没有搜索字符串的实例。Finally, the IndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.

using namespace System;

void main()
{
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };

    Console::WriteLine();
    for each(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));
}

/* This code example produces the following output:

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
 */
using System;

public class Example
{
    public static void Main()
    {
        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));
    }
}

/* This code example produces the following output:

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
 */
Imports System

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))

    End Sub
End Class

' This code example produces the following output:
'
'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

注解

如果startIndex startIndex count大于 0, 则此方法将从一维数组的元素搜索到减号1。 countThis method searches the elements of a one-dimensional array from startIndex to startIndex plus count minus 1, if count is greater than 0. 若要确定valuearray是否存在, 方法通过对每个元素调用T.Equals方法来执行相等比较。To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. 这意味着, 如果T Equals重写方法, 则将调用该重写。This means that if T overrides the Equals method, that override is called.

如果startIndex 等于Array.Length, 则此方法返回-1。If startIndex equals Array.Length, the method returns -1. 如果startIndex 大于ArgumentOutOfRangeException, 则方法将引发。 Array.LengthIf startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

此方法是 O (n) 运算, 其中ncountThis method is an O(n) operation, where n is count.

另请参阅

IndexOf<T>(T[], T) IndexOf<T>(T[], T) IndexOf<T>(T[], T) IndexOf<T>(T[], T)

在一个一维数组中搜索指定对象,并返回其首个匹配项的索引。Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value);
public static int IndexOf<T> (T[] array, T value);
static member IndexOf : 'T[] * 'T -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T) As Integer

类型参数

T

数组元素的类型。The type of the elements of the array.

参数

array
T[]

要搜索的从零开始的一维数组。The one-dimensional, zero-based array to search.

value
T T T T

要在 array 中查找的对象。The object to locate in array.

返回

如果在整个 array 中找到 value 的第一个匹配项,则为该项的从零开始的索引;否则为 -1。The zero-based index of the first occurrence of value in the entire array, if found; otherwise, -1.

异常

示例

下面的示例演示IndexOf方法的所有三个泛型重载。The following example demonstrates all three generic overloads of the IndexOf method. 创建一个字符串数组, 其中有一个条目出现在索引位置0和索引位置5。An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. IndexOf<T>(T[], T)方法重载从开始处搜索数组, 并查找字符串的第一个匹配项。The IndexOf<T>(T[], T) method overload searches the array from the beginning, and finds the first occurrence of the string. IndexOf<T>(T[], T, Int32)方法重载用于从索引位置3开始搜索数组, 并继续到数组末尾, 并查找字符串的第二个匹配项。The IndexOf<T>(T[], T, Int32) method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. 最后, IndexOf<T>(T[], T, Int32, Int32)使用方法重载搜索两个条目的范围 (从第二个索引位置开始); 它返回-1, 因为该范围中没有搜索字符串的实例。Finally, the IndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.

using namespace System;

void main()
{
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };

    Console::WriteLine();
    for each(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));
}

/* This code example produces the following output:

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
 */
using System;

public class Example
{
    public static void Main()
    {
        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));
    }
}

/* This code example produces the following output:

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
 */
Imports System

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))

    End Sub
End Class

' This code example produces the following output:
'
'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

注解

此方法搜索一维数组value的所有元素。This method searches all the elements of a one-dimensional array for value. 若要确定valuearray是否存在, 方法通过对每个元素调用T.Equals方法来执行相等比较。To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. 这意味着, 如果T Equals重写方法, 则将调用该重写。This means that if T overrides the Equals method, that override is called.

此方法为 On() 运算, 其中nLengtharrayThis method is an O(n) operation, where n is the Length of array.

另请参阅

IndexOf<T>(T[], T, Int32) IndexOf<T>(T[], T, Int32) IndexOf<T>(T[], T, Int32) IndexOf<T>(T[], T, Int32)

在一个一维数组的一系列元素中搜索指定对象,然后返回其首个匹配项的索引。Searches for the specified object in a range of elements of a one dimensional array, and returns the index of its first occurrence. 范围从指定索引到该数组结尾。The range extends from a specified index to the end of the array.

public:
generic <typename T>
 static int IndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int IndexOf<T> (T[] array, T value, int startIndex);
static member IndexOf : 'T[] * 'T * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer

类型参数

T

数组元素的类型。The type of the elements of the array.

参数

array
T[]

要搜索的从零开始的一维数组。The one-dimensional, zero-based array to search.

value
T T T T

要在 array 中查找的对象。The object to locate in array.

startIndex
Int32 Int32 Int32 Int32

从零开始的搜索的起始索引。The zero-based starting index of the search. 空数组中 0(零)为有效值。0 (zero) is valid in an empty array.

返回

如果在 value 中从 array 到最后一个元素这部分元素中找到 startIndex 的匹配项,则为第一个匹配项的从零开始的索引;否则为 -1。The zero-based index of the first occurrence of value within the range of elements in array that extends from startIndex to the last element, if found; otherwise, -1.

异常

startIndex 超出了 array 的有效索引范围。startIndex is outside the range of valid indexes for array.

示例

下面的示例演示IndexOf方法的所有三个泛型重载。The following example demonstrates all three generic overloads of the IndexOf method. 创建一个字符串数组, 其中有一个条目出现在索引位置0和索引位置5。An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. IndexOf<T>(T[], T)方法重载从开始处搜索数组, 并查找字符串的第一个匹配项。The IndexOf<T>(T[], T) method overload searches the array from the beginning, and finds the first occurrence of the string. IndexOf<T>(T[], T, Int32)方法重载用于从索引位置3开始搜索数组, 并继续到数组末尾, 并查找字符串的第二个匹配项。The IndexOf<T>(T[], T, Int32) method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. 最后, IndexOf<T>(T[], T, Int32, Int32)使用方法重载搜索两个条目的范围 (从第二个索引位置开始); 它返回-1, 因为该范围中没有搜索字符串的实例。Finally, the IndexOf<T>(T[], T, Int32, Int32) method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.

using namespace System;

void main()
{
    array<String^>^ dinosaurs = { "Tyrannosaurus", 
        "Amargasaurus",
        "Mamenchisaurus",
        "Brachiosaurus",
        "Deinonychus",
        "Tyrannosaurus",
        "Compsognathus" };

    Console::WriteLine();
    for each(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));
}

/* This code example produces the following output:

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
 */
using System;

public class Example
{
    public static void Main()
    {
        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));
    }
}

/* This code example produces the following output:

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
 */
Imports System

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs() As String = { "Tyrannosaurus", _
            "Amargasaurus", _
            "Mamenchisaurus", _
            "Brachiosaurus", _
            "Deinonychus", _
            "Tyrannosaurus", _
            "Compsognathus" }

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus"))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 3): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 3))

        Console.WriteLine(vbLf & _
            "Array.IndexOf(dinosaurs, ""Tyrannosaurus"", 2, 2): {0}", _
            Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2))

    End Sub
End Class

' This code example produces the following output:
'
'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

注解

此方法搜索从到数组末尾处startIndex的元素的一维数组。This method searches a one-dimensional array from the element at startIndex to the end of the array. 若要确定valuearray是否存在, 方法通过对每个元素调用T.Equals方法来执行相等比较。To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. 这意味着, 如果T Equals重写方法, 则将调用该重写。This means that if T overrides the Equals method, that override is called.

如果startIndex startIndex等于Length, 则此方法返回-1 Array.Length。如果大于, 则该方法将引发ArgumentOutOfRangeExceptionIf startIndex equals Length,the method returns -1.If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

此方法为 On() 运算, 其中narraystartIndex到末尾的元素数。This method is an O(n) operation, where n is the number of elements from startIndex to the end of array.

另请参阅

适用于