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.

備註

這個方法會搜尋的一維陣列的所有項目valueThis method searches all the elements of a one-dimensional array for value. 若要判斷是否value存在於array,這個方法會執行相等比較,藉由呼叫每個項目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.

因為大部分的陣列下限為零,這個方法通常會傳回-1 如果value找不到。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).

這個方法是 O (n) 運算,其中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.

傳回

如有找到,則為 array 中從 startIndex 延伸至最後一個項目的項目範圍內,第一次出現之 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. 若要判斷是否value存在於array,這個方法會執行相等比較,藉由呼叫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.

因為大部分的陣列下限為零,這個方法通常會傳回-1 如果value找不到。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大於Array.Length,方法會擲回ArgumentOutOfRangeExceptionIf startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

這個方法是 O (n) 運算,其中n中的項目數目startIndex結尾arrayThis 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.

傳回

如有找到,則為 array 中從索引 startIndexstartIndex + count -1 的範圍內,第一次出現之 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)以判斷第一個符合的字串"the"中的第四個字串陣列的最後一個項目。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.

備註

這個方法會從一維陣列的項目中搜尋startIndexstartIndex加上count減 1,如果count大於 0。This method searches the elements of a one-dimensional array from startIndex to startIndex plus count minus 1, if count is greater than 0. 若要判斷是否value存在於array,這個方法會執行相等比較,藉由呼叫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.

因為大部分的陣列下限為零,這個方法通常會傳回-1 時value找不到。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大於Array.Length,方法會擲回ArgumentOutOfRangeExceptionIf 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.

傳回

array 中從 startIndex 開始且包含 count 中指定項目數的項目範圍內,第一次出現 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

備註

這個方法會從一維陣列的項目中搜尋startIndexstartIndex加上count減 1,如果count大於 0。This method searches the elements of a one-dimensional array from startIndex to startIndex plus count minus 1, if count is greater than 0. 若要判斷是否value存在於array,這個方法會執行相等比較,藉由呼叫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大於Array.Length,方法會擲回ArgumentOutOfRangeExceptionIf 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

備註

這個方法會搜尋的一維陣列的所有項目valueThis method searches all the elements of a one-dimensional array for value. 若要判斷是否value存在於array,這個方法會執行相等比較,藉由呼叫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.

這個方法是 O (n) 運算,其中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.

傳回

array 中從 startIndex 到最後一個項目的項目範圍內,第一次出現 value 之以零為起始的索引;如未找到則為 -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. 若要判斷是否value存在於array,這個方法會執行相等比較,藉由呼叫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.

如果startIndexequals Length,則方法會傳回-1.IfstartIndex大於Array.Length,方法會擲回ArgumentOutOfRangeExceptionIf startIndex equals Length,the method returns -1.If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

這個方法是 O (n) 運算,其中n中的項目數目startIndex結尾arrayThis method is an O(n) operation, where n is the number of elements from startIndex to the end of array.

另請參閱

適用於