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

定義

指定されたオブジェクトを 1 次元の配列または配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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)

指定したオブジェクトを検索し、1 次元配列でそのオブジェクトが最初に見つかった位置のインデックス番号を返します。 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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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)

指定したオブジェクトを検索し、1 次元配列でそのオブジェクトが最初に見つかった位置のインデックス番号を返します。 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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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)

指定したオブジェクトを検索し、1 次元配列でそのオブジェクトが最初に見つかった位置のインデックス番号を返します。 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

検索する 1 次元配列。 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.

例では、次の 3 つオーバー ロードを呼び出し、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)、、文字列に最初に出現する位置を決定する文字列の配列「、」。IndexOf(Array, Object), to determine the first occurrence of the string "the" in a string array.

  • IndexOf(Array, Object, Int32)、、文字列に最初に出現する位置を決定する文字列配列の最後の要素に 4 つ目で"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)、、文字列に最初に出現する位置を決定する文字列で「、」配列の最後の一致、配列の末尾に次の場所からします。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.

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.

注釈

このメソッドは、1 次元 arrayfor のすべての要素を検索valueします。This method searches all the elements of a one-dimensional arrayfor 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.

ほとんどの配列の下限を 0 ため、このメソッド一般に-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) 操作、nは、Lengtharrayします。This 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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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

検索する 1 次元配列。 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.

戻り値

arraystartIndex から最後の要素までを範囲として 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.

例外

startIndexarray の有効なインデックスの範囲外です。 startIndex is outside the range of valid indexes for array.

array が多次元です。 array is multidimensional.

例では、次の 3 つオーバー ロードを呼び出し、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)、、文字列に最初に出現する位置を決定する文字列の配列「、」。IndexOf(Array, Object), to determine the first occurrence of the string "the" in a string array.

  • IndexOf(Array, Object, Int32)、、文字列に最初に出現する位置を決定する文字列配列の最後の要素に 4 つ目で"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)、、文字列に最初に出現する位置を決定する文字列で「、」配列の最後の一致、配列の末尾に次の場所からします。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.

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.

注釈

このメソッドは検索インデックスにある要素から 1 次元配列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.

ほとんどの配列の下限を 0 ため、このメソッド一般に-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).

場合startIndexequalsArray.Lengthメソッドは-1 を返します。If startIndex equals Array.Length,the method returns -1. 場合startIndexがより大きいArray.Lengthがスローされます、ArgumentOutOfRangeExceptionします。If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

このメソッドは O (n) 操作、nから要素の数は、startIndexの末尾にarrayします。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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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

検索する 1 次元配列。 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.

戻り値

インデックス startIndex から startIndex + 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.

例外

startIndexarray の有効なインデックスの範囲外です。 startIndex is outside the range of valid indexes for array. - または - -or- count が 0 未満です。 count is less than zero. - または - -or- startIndex および countarray 内の有効なセクションを指定していません。 startIndex and count do not specify a valid section in array.

array が多次元です。 array is multidimensional.

例では、次の 3 つオーバー ロードを呼び出し、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)、、文字列に最初に出現する位置を決定する文字列の配列「、」。IndexOf(Array, Object), to determine the first occurrence of the string "the" in a string array.

  • IndexOf(Array, Object, Int32)、、文字列に最初に出現する位置を決定する文字列配列の最後の要素に 4 つ目で"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)、、文字列に最初に出現する位置を決定する文字列で「、」配列の最後の一致、配列の末尾に次の場所からします。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引数、開始インデックスから配列の上限値を減算し、1 つ追加します。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.

注釈

このメソッドは、1 次元 arrayfrom の要素を検索startIndexstartIndexpluscountから 1 を引いた場合countが 0 より大きい。This method searches the elements of a one-dimensional arrayfrom 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.

Becausemost 配列が下限を 0 が、一般に、このメソッドは – 1 の場合を返しますvalueが見つからない。Becausemost 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).

場合startindexequalsArray.Lengthメソッドは-1 を返します。If startindex equals Array.Length, the method returns -1. 場合startIndexがより大きいArray.Lengthがスローされます、ArgumentOutOfRangeExceptionします。If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

このメソッドは O (n) 操作、ncountします。This 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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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[]

検索する 1 次元の配列。インデックス番号が 0 から始まる必要があります。 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

検索の開始位置を示す 0 から始まるインデックス。 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.

戻り値

valuearray から startIndex 個の要素を範囲として count を検索し、見つかった場合は、インデックス番号の最も小さい要素の 0 から始まるインデックス番号。それ以外の場合は –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.

例外

startIndexarray の有効なインデックスの範囲外です。 startIndex is outside the range of valid indexes for array. - または - -or- count が 0 未満です。 count is less than zero. - または - -or- startIndex および countarray 内の有効なセクションを指定していません。 startIndex and count do not specify a valid section in array.

次の例では、すべて次の 3 つのジェネリック オーバー ロード、IndexOfメソッド。The following example demonstrates all three generic overloads of the IndexOf method. インデックス位置 0 と 5 のインデックスの場所に 2 回、表示する 1 つのエントリで、文字列の配列が作成されます。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 以降と、配列の最後の検索し、文字列の 2 つ目の項目を検索します。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)は 2 つのインデックス位置にある先頭の 2 つのエントリの範囲を検索するメソッドのオーバー ロードが使用されます。 その範囲内で検索文字列のインスタンスがないため、-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

注釈

このメソッドの searchesthe 要素から 1 次元配列のstartIndexstartIndexpluscountから 1 を引いた場合countが 0 より大きい。This method searchesthe 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.

場合startIndexequalsArray.Lengthメソッドは-1 を返します。If startIndex equals Array.Length, the method returns -1. 場合startIndexがより大きいArray.Lengthがスローされます、ArgumentOutOfRangeExceptionします。If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

このメソッドは O (n) 操作、ncountします。This 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)

指定したオブジェクトを検索し、1 次元配列でそのオブジェクトが最初に見つかった位置のインデックス番号を返します。 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[]

検索する 1 次元の配列。インデックス番号が 0 から始まる必要があります。 The one-dimensional, zero-based array to search.

value
T T T T

array 内で検索するオブジェクト。 The object to locate in array.

戻り値

array 全体を対象とする value の検索で最初に見つかった位置のインデックス (0 から始まる)。それ以外の場合は -1。 The zero-based index of the first occurrence of value in the entire array, if found; otherwise, –1.

例外

次の例では、すべて次の 3 つのジェネリック オーバー ロード、IndexOfメソッド。The following example demonstrates all three generic overloads of the IndexOf method. インデックス位置 0 と 5 のインデックスの場所に 2 回、表示する 1 つのエントリで、文字列の配列が作成されます。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 以降と、配列の最後の検索し、文字列の 2 つ目の項目を検索します。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)は 2 つのインデックス位置にある先頭の 2 つのエントリの範囲を検索するメソッドのオーバー ロードが使用されます。 その範囲内で検索文字列のインスタンスがないため、-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

注釈

このメソッドの 1 次元配列のすべての要素を検索するvalueします。This 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) 操作、nは、Lengtharrayします。This 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)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 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[]

検索する 1 次元の配列。インデックス番号が 0 から始まる必要があります。 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

検索の開始位置を示す 0 から始まるインデックス。 The zero-based starting index of the search. 空の配列の場合 0 (ゼロ) は有効です。 0 (zero) is valid in an empty array.

戻り値

valuearray から最後の要素までを範囲として startIndex を検索し、見つかった場合は、インデックス番号の最も小さい要素の 0 から始まるインデックス番号。それ以外の場合は –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.

例外

startIndexarray の有効なインデックスの範囲外です。 startIndex is outside the range of valid indexes for array.

次の例では、すべて次の 3 つのジェネリック オーバー ロード、IndexOfメソッド。The following example demonstrates all three generic overloads of the IndexOf method. インデックス位置 0 と 5 のインデックスの場所に 2 回、表示する 1 つのエントリで、文字列の配列が作成されます。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 以降と、配列の最後の検索し、文字列の 2 つ目の項目を検索します。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)は 2 つのインデックス位置にある先頭の 2 つのエントリの範囲を検索するメソッドのオーバー ロードが使用されます。 その範囲内で検索文字列のインスタンスがないため、-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

注釈

このメソッドを 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がスローされます、ArgumentOutOfRangeExceptionします。If startIndex equals Length,the method returns -1.If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

このメソッドは O (n) 操作、nから要素の数は、startIndexの末尾にarrayします。This method is an O(n) operation, where n is the number of elements from startIndex to the end of array.

こちらもご覧ください

適用対象