次の方法で共有


Array.IndexOf メソッド

定義

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

オーバーロード

名前 説明
IndexOf(Array, Object)

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

IndexOf(Array, Object, Int32)

1 次元配列の要素範囲で指定したオブジェクトを検索し、最初に出現するオブジェクトのインデックスを返します。 範囲は、指定したインデックスから配列の末尾まで拡張されます。

IndexOf(Array, Object, Int32, Int32)

1 次元配列の要素範囲で指定したオブジェクトを検索し、最初に出現する if のインデックスを返します。 範囲は、指定した数の要素の指定したインデックスから拡張されます。

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

1 次元配列の要素範囲で指定されたオブジェクトを検索し、最初に出現するオブジェクトのインデックスを返します。 範囲は、指定したインデックスから配列の末尾まで拡張されます。

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

1 次元配列の要素範囲で指定したオブジェクトを検索し、最初に出現するオブジェクトのインデックスを返します。 範囲は、指定した数の要素の指定したインデックスから拡張されます。

IndexOf<T>(T[], T)

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

IndexOf(Array, Object)

ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
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で検索するオブジェクト。

戻り値

arrayで最初に出現するvalueのインデックス(見つかった場合)、それ以外の場合は配列の下限から 1 を引いた値。

例外

arraynullです。

array は多次元です。

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

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

  • IndexOf(Array, Object, Int32): 文字列配列の 4 番目から最後の要素への文字列 "the" の最初の出現を決定します。

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

// 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します。 arrayvalueが存在するかどうかを判断するために、メソッドは、既定の等値比較子EqualityComparer<T>.Defaultを使用して等価比較を実行します。

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

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

こちらもご覧ください

適用対象

IndexOf(Array, Object, Int32)

ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
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 (ゼロ) は空の配列で有効です。

戻り値

最初に出現するvalueのインデックス 。見つかった場合は、startIndexから最後の要素までのarray内の要素の範囲内です。それ以外の場合は、配列の下限から 1 を引きます。

例外

arraynullです。

startIndex が、 arrayの有効なインデックスの範囲外です。

array は多次元です。

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

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

  • IndexOf(Array, Object, Int32): 文字列配列の 4 番目から最後の要素への文字列 "the" の最初の出現を決定します。

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

// 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 次元配列を検索します。 arrayvalueが存在するかどうかを判断するために、メソッドは、既定の等値比較子EqualityComparer<T>.Defaultを使用して等価比較を実行します。

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

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

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

こちらもご覧ください

適用対象

IndexOf(Array, Object, Int32, Int32)

ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs

1 次元配列の要素範囲で指定したオブジェクトを検索し、最初に出現する if のインデックスを返します。 範囲は、指定した数の要素の指定したインデックスから拡張されます。

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です。

startIndex が、 arrayの有効なインデックスの範囲外です。

-又は-

count が 0 未満です。

-又は-

startIndexcount は、 arrayで有効なセクションを指定しません。

array は多次元です。

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

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

  • IndexOf(Array, Object, Int32): 文字列配列の 4 番目から最後の要素への文字列 "the" の最初の出現を決定します。

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

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

注釈

このメソッドは、countが 0 より大きい場合に、startIndexから startIndexcount - 1 までの 1 次元配列の要素を検索します。 arrayvalueが存在するかどうかを判断するために、メソッドは、既定の等値比較子EqualityComparer<T>.Defaultを使用して等価比較を実行します。

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

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

このメソッドは O(n) 操作であり、 ncount

こちらもご覧ください

適用対象

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

ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
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 (ゼロ) は空の配列で有効です。

戻り値

startIndexから最後の要素まで続くarray内で最初に出現するvalueの 0 から始まるインデックス 。それ以外の場合は -1。

例外

arraynullです。

startIndex が、 arrayの有効なインデックスの範囲外です。

次の例では、 IndexOf メソッドの 3 つのジェネリック オーバーロードをすべて示します。 インデックス位置 0 とインデックス位置 5 に、1 つのエントリが 2 回表示される文字列の配列が作成されます。 IndexOf<T>(T[], T) メソッドのオーバーロードは、最初から配列を検索し、文字列の最初の出現箇所を検索します。 IndexOf<T>(T[], T, Int32) メソッドのオーバーロードは、インデックス位置 3 で始まり、配列の末尾まで続く配列を検索するために使用され、文字列の 2 番目の出現箇所を検索します。 最後に、 IndexOf<T>(T[], T, Int32, Int32) メソッドのオーバーロードは、インデックス位置 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 次元配列を検索します。 arrayvalueが存在するかどうかを判断するために、メソッドは、既定の等値比較子EqualityComparer<T>.Defaultを使用して等価比較を実行します。

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

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

こちらもご覧ください

適用対象

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

ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
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

検索するセクション内の要素の数。

戻り値

startIndexから始まり、countで指定された要素の数を含む、array内の要素の範囲内で最初に出現するvalueの 0 から始まるインデックス。それ以外の場合は -1。

例外

arraynullです。

startIndex が、 arrayの有効なインデックスの範囲外です。

-又は-

count が 0 未満です。

-又は-

startIndexcount は、 arrayで有効なセクションを指定しません。

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

注釈

このメソッドは、countが 0 より大きい場合に、startIndexから startIndexcount - 1 までの 1 次元配列の要素を検索します。 arrayvalueが存在するかどうかを判断するために、メソッドは、既定の等値比較子EqualityComparer<T>.Defaultを使用して等価比較を実行します。

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

このメソッドは O(n) 操作であり、 ncount

こちらもご覧ください

適用対象

IndexOf<T>(T[], T)

ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
Array.cs
ソース:
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です。

次の例では、 IndexOf メソッドの 3 つのジェネリック オーバーロードをすべて示します。 インデックス位置 0 とインデックス位置 5 に、1 つのエントリが 2 回表示される文字列の配列が作成されます。 IndexOf<T>(T[], T) メソッドのオーバーロードは、最初から配列を検索し、文字列の最初の出現箇所を検索します。 IndexOf<T>(T[], T, Int32) メソッドのオーバーロードは、インデックス位置 3 で始まり、配列の末尾まで続く配列を検索するために使用され、文字列の 2 番目の出現箇所を検索します。 最後に、 IndexOf<T>(T[], T, Int32, Int32) メソッドのオーバーロードは、インデックス位置 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します。 arrayvalueが存在するかどうかを判断するために、メソッドは、既定の等値比較子EqualityComparer<T>.Defaultを使用して等価比較を実行します。

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

こちらもご覧ください

適用対象