Array.IndexOf メソッド

定義

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

オーバーロード

IndexOf(Array, Object)

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

IndexOf(Array, Object, Int32)

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

IndexOf(Array, Object, Int32, Int32)

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 範囲は、指定されたインデックスから始まり、指定された数の要素を含む範囲です。

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

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

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

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 範囲は、指定されたインデックスから始まり、指定された数の要素を含む範囲です。

IndexOf<T>(T[], T)

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

IndexOf(Array, Object)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public:
 static int IndexOf(Array ^ array, System::Object ^ value);
public static int IndexOf (Array array, object value);
public static int IndexOf (Array array, object? value);
static member IndexOf : Array * obj -> int
Public Shared Function IndexOf (array As Array, value As Object) As Integer

パラメーター

array
Array

検索対象となる 1 次元配列。

value
Object

array 内で検索するオブジェクト。

戻り値

arrayvalue が見つかった場合は、最初に見つかった位置のインデックス。それ以外の場合は、配列の下限 - 1。

例外

arraynullです。

array が多次元です。

この例では、 メソッドの次の 3 つのオーバーロードを IndexOf 呼び出して、文字列配列内の文字列のインデックスを検索します。

  • IndexOf(Array, Object)は、文字列配列内の文字列 "the" の最初の出現を決定します。

  • IndexOf(Array, Object, Int32)は、文字列配列の最後の要素の 4 番目の要素で文字列 "the" が最初に出現することを決定します。

  • IndexOf(Array, Object, Int32, Int32)は、最後に成功した配列の末尾に一致した要素から文字列配列内で文字列 "the" が最初に出現することを決定します。

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.
// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]

// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"

// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."

// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."

// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {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.
// 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 次元配列のすべての要素で を検索します value。 が にarray存在するかどうかをvalue判断するために、 メソッドは、一致が見つかるまで各要素のEqualsメソッドを呼び出して等値比較を実行します。 つまり、要素が メソッドをオーバーライドすると Object.Equals(Object) 、そのオーバーライドが呼び出されます。

ほとんどの配列の下限は 0 であるため、このメソッドは通常、見つからない場合value は -1 を返します。 まれに、配列の下限が (0x80000000) valueInt32.MinValue等しく、見つからない場合、このメソッドは (0x7FFFFFFF) を返しますInt32.MaxValue

このメソッドは O(n) 操作です。ここで nLength は の arrayです。

こちらもご覧ください

適用対象

IndexOf(Array, Object, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

public:
 static int IndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int IndexOf (Array array, object value, int startIndex);
public static int IndexOf (Array array, object? value, int startIndex);
static member IndexOf : Array * obj * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer) As Integer

パラメーター

array
Array

検索対象となる 1 次元配列。

value
Object

array 内で検索するオブジェクト。

startIndex
Int32

検索の開始インデックス。 空の配列の場合 0 (ゼロ) は有効です。

戻り値

arraystartIndex から最後の要素までの範囲内で value が見つかった場合は、最初に見つかった位置のインデックス。それ以外の場合は、配列の下限 - 1。

例外

arraynullです。

startIndexarray の有効なインデックスの範囲外です。

array が多次元です。

この例では、 メソッドの次の 3 つのオーバーロードを IndexOf 呼び出して、文字列配列内の文字列のインデックスを検索します。

  • IndexOf(Array, Object)は、文字列配列内の文字列 "the" の最初の出現を決定します。

  • IndexOf(Array, Object, Int32)は、文字列配列の最後の要素の 4 番目の要素で文字列 "the" が最初に出現することを決定します。

  • IndexOf(Array, Object, Int32, Int32)は、最後に成功した配列の末尾に一致した要素から文字列配列内で文字列 "the" が最初に出現することを決定します。

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.
// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]

// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"

// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."

// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."

// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {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.
// 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 位置の要素から最後の要素までの 1 次元配列を検索します。 が にarray存在するかどうかをvalue判断するために、 メソッドは、一致が見つかるまですべての要素の メソッドをEquals呼び出して等価比較を実行します。 つまり、要素が メソッドをオーバーライドすると Object.Equals(Object) 、そのオーバーライドが呼び出されます。

ほとんどの配列の下限は 0 であるため、このメソッドは通常、見つからない場合 value は -1 を返します。 まれに、配列の下限が (0x80000000) valueInt32.MinValue等しく、見つからない場合、このメソッドは (0x7FFFFFFF) を返しますInt32.MaxValue

が にArray.Length等しい場合startIndex、メソッドは -1 を返します。 が よりArray.Length大きい場合startIndex、メソッドは をスローしますArgumentOutOfRangeException

このメソッドは O(n) 操作です。ここで n 、 は から startIndex の末尾までの要素の array数です。

こちらもご覧ください

適用対象

IndexOf(Array, Object, Int32, Int32)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 範囲は、指定されたインデックスから始まり、指定された数の要素を含む範囲です。

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);
public static int IndexOf (Array array, object? value, int startIndex, int count);
static member IndexOf : Array * obj * int * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer

パラメーター

array
Array

検索対象となる 1 次元配列。

value
Object

array 内で検索するオブジェクト。

startIndex
Int32

検索の開始インデックス。 空の配列の場合 0 (ゼロ) は有効です。

count
Int32

検索する要素数。

戻り値

インデックス startIndex から startIndex + count - 1 までの array 内で value が見つかった場合は、最初に見つかった位置のインデックス。それ以外の場合は、配列の下限 - 1。

例外

arraynullです。

startIndexarray の有効なインデックスの範囲外です。

または

count が 0 未満です。

または

startIndex および countarray 内の有効なセクションを指定していません。

array が多次元です。

この例では、 メソッドの次の 3 つのオーバーロードを IndexOf 呼び出して、文字列配列内の文字列のインデックスを検索します。

  • IndexOf(Array, Object)は、文字列配列内の文字列 "the" の最初の出現を決定します。

  • IndexOf(Array, Object, Int32)は、文字列配列の最後の要素の 4 番目の要素で文字列 "the" が最初に出現することを決定します。

  • IndexOf(Array, Object, Int32, Int32)は、最後に成功した配列の末尾に一致した要素から文字列配列内で文字列 "the" が最初に出現することを決定します。 引数の値を count 決定するために、開始インデックスから配列の上限を減算し、1 つを追加します。

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.
// Create a string array with 3 elements having the same value.
let strings = 
    [| "the"; "quick"; "brown"; "fox"; "jumps"; "over"
       "the"; "lazy"; "dog"; "in"; "the"; "barn" |]

// Display the elements of the array.
printfn "The array contains the following values:"
for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do
    printfn $"   [{i,2}]: {strings[i]}"

// Search for the first occurrence of the duplicated value.
let searchString = "the"
let index = Array.IndexOf(strings, searchString)
printfn $"The first occurrence of \"{searchString}\" is at index {index}."

// Search for the first occurrence of the duplicated value in the last section of the array.
let index = Array.IndexOf(strings, searchString, 4)
printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}."

// Search for the first occurrence of the duplicated value in a section of the array.
let position = index + 1
let index = Array.IndexOf(strings, searchString, position, strings.GetUpperBound 0 - position + 1)
printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {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.
// 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.

注釈

このメソッドは、 が 0 より大きい場合count、1 次元配列startIndexの要素を startIndex から + count 1 を引いた値まで検索します。 が にarray存在するかどうかをvalue判断するために、 メソッドは、一致が見つかるまですべての要素の メソッドをEquals呼び出して等価比較を実行します。 つまり、要素が メソッドをオーバーライドすると Object.Equals 、そのオーバーライドが呼び出されます。

ほとんどの配列の下限は 0 であるため、このメソッドは通常、 が見つからない場合 value は -1 を返します。 まれに、配列の下限が (0x80000000) valueInt32.MinValue等しく、見つからない場合、このメソッドは (0x7FFFFFFF) を返しますInt32.MaxValue

が とArray.Length等しい場合startindex、メソッドは -1 を返します。 が よりArray.Length大きい場合startIndex、メソッドは をスローしますArgumentOutOfRangeException

このメソッドは O(n) 操作です。ncount です。

こちらもご覧ください

適用対象

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

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

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

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の配列。インデックス番号が 0 から始まる必要があります。

value
T

array 内で検索するオブジェクト。

startIndex
Int32

検索の開始位置を示す 0 から始まるインデックス。 空の配列の場合 0 (ゼロ) は有効です。

戻り値

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

例外

arraynullです。

startIndexarray の有効なインデックスの範囲外です。

次の例では、 メソッドの 3 つのジェネリック オーバーロードをすべて示します IndexOf 。 文字列の配列が作成され、1 つのエントリがインデックス位置 0 とインデックス位置 5 に 2 回表示されます。 メソッド オーバーロードは IndexOf<T>(T[], T) 、最初から配列を検索し、文字列の最初の出現箇所を検索します。 IndexOf<T>(T[], T, Int32)メソッドオーバーロードは、インデックス位置 3 で始まり、配列の末尾まで続く配列を検索するために使用され、文字列の 2 番目の出現箇所を検索します。 最後に、メソッド オーバーロードは、 IndexOf<T>(T[], T, Int32, Int32) インデックス位置 2 から始まる 2 つのエントリの範囲を検索するために使用されます。その範囲内に検索文字列のインスタンスがないため、-1 が返されます。

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

let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"

// 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
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 から配列の末尾までの 1 次元配列を検索します。 に arrayが存在するかどうかをvalue判断するために、 メソッドは、すべての要素で メソッドを呼び出して等値比較をT.Equals実行します。 つまり、 T メソッドをオーバーライドすると Equals 、そのオーバーライドが呼び出されます。

が とLength等しい場合startIndex、メソッドは -1 を返します。 が よりArray.Length大きい場合startIndex、メソッドは をArgumentOutOfRangeExceptionスローします。

このメソッドは O(n) 操作です。ここで n 、 は から の末尾までの要素 startIndexarray数です。

こちらもご覧ください

適用対象

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

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

指定されたオブジェクトを 1 次元配列の要素範囲内で検索し、最初に見つかったオブジェクトのインデックスを返します。 範囲は、指定されたインデックスから始まり、指定された数の要素を含む範囲です。

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

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の配列。インデックス番号が 0 から始まる必要があります。

value
T

array 内で検索するオブジェクト。

startIndex
Int32

検索の開始位置を示す 0 から始まるインデックス。 空の配列の場合 0 (ゼロ) は有効です。

count
Int32

検索対象の範囲内にある要素の数。

戻り値

arraystartIndex から count 個の要素を範囲として value を検索し、見つかった場合は、インデックス番号の最も小さい要素の 0 から始まるインデックス番号。それ以外の場合は -1。

例外

arraynullです。

startIndexarray の有効なインデックスの範囲外です。

または

count が 0 未満です。

または

startIndex および countarray 内の有効なセクションを指定していません。

次の例では、 メソッドの 3 つのジェネリック オーバーロードすべてを示します IndexOf 。 文字列の配列が作成され、インデックス位置 0 とインデックス位置 5 に 1 つのエントリが 2 回表示されます。 メソッド オーバーロードは IndexOf<T>(T[], T) 、最初から配列を検索し、文字列の最初の出現箇所を検索します。 メソッド オーバーロードは IndexOf<T>(T[], T, Int32) 、インデックス位置 3 で始まり、配列の末尾まで続く配列を検索するために使用され、文字列の 2 番目の出現箇所を検索します。 最後に、 IndexOf<T>(T[], T, Int32, Int32) インデックス位置 2 から始まる 2 つのエントリの範囲を検索するためにメソッド オーバーロードが使用されます。その範囲内に検索文字列のインスタンスがないため、-1 が返されます。

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

let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"

// 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
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

注釈

このメソッドは、 が 0 より大きい場合count、1 次元配列の要素を から startIndex + 1 を加算countしてstartIndex検索します。 に arrayが存在するかどうかをvalue判断するために、 メソッドは、すべての要素で メソッドを呼び出して等値比較をT.Equals実行します。 つまり、 T メソッドをオーバーライドすると Equals 、そのオーバーライドが呼び出されます。

が とArray.Length等しい場合startIndex、メソッドは -1 を返します。 が よりArray.Length大きい場合startIndex、メソッドは をArgumentOutOfRangeExceptionスローします。

このメソッドは O(n) 操作です。ncount です。

こちらもご覧ください

適用対象

IndexOf<T>(T[], T)

Source:
Array.cs
Source:
Array.cs
Source:
Array.cs

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

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

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の配列。インデックス番号が 0 から始まる必要があります。

value
T

array 内で検索するオブジェクト。

戻り値

array 全体を対象とする value の検索で最初に見つかった位置のインデックス (0 から始まる)。それ以外の場合は -1。

例外

arraynullです。

次の例では、 メソッドの 3 つのジェネリック オーバーロードすべてを示します IndexOf 。 文字列の配列が作成され、インデックス位置 0 とインデックス位置 5 に 1 つのエントリが 2 回表示されます。 メソッド オーバーロードは IndexOf<T>(T[], T) 、最初から配列を検索し、文字列の最初の出現箇所を検索します。 メソッド オーバーロードは IndexOf<T>(T[], T, Int32) 、インデックス位置 3 で始まり、配列の末尾まで続く配列を検索するために使用され、文字列の 2 番目の出現箇所を検索します。 最後に、 IndexOf<T>(T[], T, Int32, Int32) インデックス位置 2 から始まる 2 つのエントリの範囲を検索するためにメソッド オーバーロードが使用されます。その範囲内に検索文字列のインスタンスがないため、-1 が返されます。

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

let dinosaurs =
    [| "Tyrannosaurus"
       "Amargasaurus"
       "Mamenchisaurus"
       "Brachiosaurus"
       "Deinonychus"
       "Tyrannosaurus"
       "Compsognathus" |]

printfn ""
for dino in dinosaurs do
    printfn $"{dino}"

Array.IndexOf(dinosaurs, "Tyrannosaurus")
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\"): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 3)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 3): %i"

Array.IndexOf(dinosaurs, "Tyrannosaurus", 2, 2)
|> printfn "\nArray.IndexOf(dinosaurs, \"Tyrannosaurus\", 2, 2): %i"

// 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
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。 に arrayが存在するかどうかをvalue判断するために、 メソッドは、すべての要素で メソッドを呼び出して等値比較をT.Equals実行します。 つまり、 T メソッドをオーバーライドすると Equals 、そのオーバーライドが呼び出されます。

このメソッドは O(n) 操作です。ここで nLength は の arrayです。

こちらもご覧ください

適用対象