Array.LastIndexOf メソッド

定義

1 次元 Array 内または Array の範囲内で値が最後に見つかった位置のインデックス番号を返します。

オーバーロード

LastIndexOf(Array, Object)

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

LastIndexOf(Array, Object, Int32)

指定したオブジェクトを、1 次元 Array の先頭の要素から、指定されたインデックス位置までを範囲として検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

LastIndexOf(Array, Object, Int32, Int32)

指定したオブジェクトを、1 次元 Array の指定したインデックス位置から、指定した要素数を範囲として検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

LastIndexOf<T>(T[], T)

指定したオブジェクトを Array 全体を対象に検索し、インデックス番号の最も大きい要素のインデックスを返します。

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

指定したオブジェクトを、Array の先頭の要素から、指定されたインデックス位置までを範囲として検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

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

指定したオブジェクトを、指定した数の要素を含み、かつ指定したインデックスで終了する Array の要素の範囲内で検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

LastIndexOf(Array, Object)

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

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

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

パラメーター

array
Array

検索対象となる 1 次元 Array

value
Object

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

戻り値

array 全体を対象に value を検索し、見つかった場合は、インデックス番号の最も大きい要素のインデックス番号。それ以外の場合は、配列の下限 - 1。

例外

arraynullです。

array が多次元です。

次のコード例は、配列内で指定された要素が最後に出現するインデックスを決定する方法を示しています。

using namespace System;
void PrintIndexAndValues( Array^ myArray );

void main()
{
   // Creates and initializes a new Array instance with three elements of the same value.
   Array^ myArray = Array::CreateInstance( String::typeid, 12 );
   myArray->SetValue( "the", 0 );
   myArray->SetValue( "quick", 1 );
   myArray->SetValue( "brown", 2 );
   myArray->SetValue( "fox", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );
   myArray->SetValue( "in", 9 );
   myArray->SetValue( "the", 10 );
   myArray->SetValue( "barn", 11 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance contains the following values:" );
   PrintIndexAndValues( myArray );

   // Searches for the last occurrence of the duplicated value.
   String^ myString =  "the";
   int myIndex = Array::LastIndexOf( myArray, myString );
   Console::WriteLine(  "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in the first section of the Array.
   myIndex = Array::LastIndexOf( myArray, myString, 8 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in a section of the Array.  
   // Note that the start index is greater than the end index because the search is done backward.
   myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance 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 last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */
let printIndexAndValues (arr: 'a []) =
   for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
      printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array with three elements of the same value.
let myArray = 
   [| "the"; "quick"; "brown"; "fox"
      "jumps"; "over"; "the"; "lazy"
      "dog"; "in"; "the"; "barn" |]

// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray

// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."


//      This code produces 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 last occurrence of "the" is at index 10.
//      The last occurrence of "the" between the start and index 8 is at index 6.
//      The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array with three elements of
        ' the same value.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
        myArray.SetValue("the", 0)
        myArray.SetValue("quick", 1)
        myArray.SetValue("brown", 2)
        myArray.SetValue("fox", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        myArray.SetValue("in", 9)
        myArray.SetValue("the", 10)
        myArray.SetValue("barn", 11)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintIndexAndValues(myArray)
        
        ' Searches for the last occurrence of the duplicated value.
        Dim myString As String = "the"
        Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
        Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
           myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in the first
        ' section of the Array.
        myIndex = Array.LastIndexOf(myArray, myString, 8)
        Console.WriteLine("The last occurrence of ""{0}"" between the start " _
           + "and index 8 is at index {1}.", myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in a section
        ' of the Array.  Note that the start index is greater than the end
        ' index because the search is done backward.
        myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
        Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
           + "and index 10 is at index {1}.", myString, myIndex)
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces 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 last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.

注釈

1 次元 Array は、最後の要素から始まり、最初の要素で終わる後方に検索されます。

要素は、 メソッドを使用して指定された値と Object.Equals 比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、 Equals その型の実装が使用されます。

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

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

.NET Framework 2.0 以降のバージョンでは、このメソッドは の メソッドと CompareTo メソッドを使用Equalsして、 パラメーターでvalue指定された がObject存在Arrayするかどうかを判断します。 以前のバージョンの.NET Frameworkでは、この決定は それ自体の Equals メソッドと CompareTo メソッドをvalueObject使用して行われました。

CompareTo コレクション内の item オブジェクトの パラメーターの メソッド。

こちらもご覧ください

適用対象

LastIndexOf(Array, Object, Int32)

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

指定したオブジェクトを、1 次元 Array の先頭の要素から、指定されたインデックス位置までを範囲として検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

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

パラメーター

array
Array

検索対象となる 1 次元 Array

value
Object

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

startIndex
Int32

逆方向検索の開始インデックス。

戻り値

array の先頭の要素から startIndex までを範囲として value を検索し、見つかった場合は、インデックス番号の最も大きい要素のインデックス番号。それ以外の場合は、配列の下限 - 1。

例外

arraynullです。

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

array が多次元です。

次のコード例は、配列内で指定された要素が最後に出現するインデックスを決定する方法を示しています。

using namespace System;
void PrintIndexAndValues( Array^ myArray );

void main()
{
   // Creates and initializes a new Array instance with three elements of the same value.
   Array^ myArray = Array::CreateInstance( String::typeid, 12 );
   myArray->SetValue( "the", 0 );
   myArray->SetValue( "quick", 1 );
   myArray->SetValue( "brown", 2 );
   myArray->SetValue( "fox", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );
   myArray->SetValue( "in", 9 );
   myArray->SetValue( "the", 10 );
   myArray->SetValue( "barn", 11 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance contains the following values:" );
   PrintIndexAndValues( myArray );

   // Searches for the last occurrence of the duplicated value.
   String^ myString =  "the";
   int myIndex = Array::LastIndexOf( myArray, myString );
   Console::WriteLine(  "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in the first section of the Array.
   myIndex = Array::LastIndexOf( myArray, myString, 8 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in a section of the Array.  
   // Note that the start index is greater than the end index because the search is done backward.
   myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance 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 last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */
let printIndexAndValues (arr: 'a []) =
   for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
      printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array with three elements of the same value.
let myArray = 
   [| "the"; "quick"; "brown"; "fox"
      "jumps"; "over"; "the"; "lazy"
      "dog"; "in"; "the"; "barn" |]

// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray

// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."


//      This code produces 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 last occurrence of "the" is at index 10.
//      The last occurrence of "the" between the start and index 8 is at index 6.
//      The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array with three elements of
        ' the same value.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
        myArray.SetValue("the", 0)
        myArray.SetValue("quick", 1)
        myArray.SetValue("brown", 2)
        myArray.SetValue("fox", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        myArray.SetValue("in", 9)
        myArray.SetValue("the", 10)
        myArray.SetValue("barn", 11)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintIndexAndValues(myArray)
        
        ' Searches for the last occurrence of the duplicated value.
        Dim myString As String = "the"
        Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
        Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
           myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in the first
        ' section of the Array.
        myIndex = Array.LastIndexOf(myArray, myString, 8)
        Console.WriteLine("The last occurrence of ""{0}"" between the start " _
           + "and index 8 is at index {1}.", myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in a section
        ' of the Array.  Note that the start index is greater than the end
        ' index because the search is done backward.
        myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
        Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
           + "and index 10 is at index {1}.", myString, myIndex)
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces 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 last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.

注釈

1 次元 Array は、最初の要素から startIndex 始まり、最後まで後方に検索されます。

要素は、 メソッドを使用して指定された値と Object.Equals 比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、 Equals その型の実装が使用されます。

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

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

.NET Framework 2.0 以降のバージョンでは、このメソッドは の メソッドと CompareTo メソッドを使用Equalsして、 パラメーターでvalue指定された がObject存在Arrayするかどうかを判断します。 以前のバージョンの.NET Frameworkでは、この決定は それ自体の Equals メソッドと CompareTo メソッドをvalueObject使用して行われました。

こちらもご覧ください

適用対象

LastIndexOf(Array, Object, Int32, Int32)

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

指定したオブジェクトを、1 次元 Array の指定したインデックス位置から、指定した要素数を範囲として検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

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

パラメーター

array
Array

検索対象となる 1 次元 Array

value
Object

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

startIndex
Int32

逆方向検索の開始インデックス。

count
Int32

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

戻り値

countstartIndex から、array で指定された要素数を範囲として value を検索し、見つかった場合は、インデックス番号の最も大きい要素のインデックス番号。それ以外の場合は、配列の下限 - 1。

例外

arraynullです。

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

または

count が 0 未満です。

または

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

array が多次元です。

次のコード例は、配列内で指定された要素が最後に出現するインデックスを決定する方法を示しています。 メソッドは後方検索であるため LastIndexOfcount (startIndex から配列の下限と 1 を引いた値) 以下である必要があります。

using namespace System;
void PrintIndexAndValues( Array^ myArray );

void main()
{
   // Creates and initializes a new Array instance with three elements of the same value.
   Array^ myArray = Array::CreateInstance( String::typeid, 12 );
   myArray->SetValue( "the", 0 );
   myArray->SetValue( "quick", 1 );
   myArray->SetValue( "brown", 2 );
   myArray->SetValue( "fox", 3 );
   myArray->SetValue( "jumps", 4 );
   myArray->SetValue( "over", 5 );
   myArray->SetValue( "the", 6 );
   myArray->SetValue( "lazy", 7 );
   myArray->SetValue( "dog", 8 );
   myArray->SetValue( "in", 9 );
   myArray->SetValue( "the", 10 );
   myArray->SetValue( "barn", 11 );

   // Displays the values of the Array.
   Console::WriteLine(  "The Array instance contains the following values:" );
   PrintIndexAndValues( myArray );

   // Searches for the last occurrence of the duplicated value.
   String^ myString =  "the";
   int myIndex = Array::LastIndexOf( myArray, myString );
   Console::WriteLine(  "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in the first section of the Array.
   myIndex = Array::LastIndexOf( myArray, myString, 8 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

   // Searches for the last occurrence of the duplicated value in a section of the Array.  
   // Note that the start index is greater than the end index because the search is done backward.
   myIndex = Array::LastIndexOf( myArray, myString, 10, 6 );
   Console::WriteLine(  "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine(  "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
 This code produces the following output.
 
 The Array instance 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 last occurrence of "the" is at index 10.
 The last occurrence of "the" between the start and index 8 is at index 6.
 The last occurrence of "the" between index 5 and index 10 is at index 10.
 */
let printIndexAndValues (arr: 'a []) =
   for i = arr.GetLowerBound 0 to arr.GetUpperBound 0 do
      printfn $"\t[{i}]:\t{arr[i]}"

// Creates and initializes a new Array with three elements of the same value.
let myArray = 
   [| "the"; "quick"; "brown"; "fox"
      "jumps"; "over"; "the"; "lazy"
      "dog"; "in"; "the"; "barn" |]

// Displays the values of the Array.
printfn "The Array contains the following values:"
printIndexAndValues myArray

// Searches for the last occurrence of the duplicated value.
let myString = "the"
let myIndex = Array.LastIndexOf(myArray, myString)
printfn $"The last occurrence of \"{myString}\" is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in the first section of the Array.
let myIndex = Array.LastIndexOf(myArray, myString, 8)
printfn $"The last occurrence of \"{myString}\" between the start and index 8 is at index {myIndex}."

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
let myIndex = Array.LastIndexOf( myArray, myString, 10, 6 )
printfn $"The last occurrence of \"{myString}\" between index 5 and index 10 is at index {myIndex}."


//      This code produces 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 last occurrence of "the" is at index 10.
//      The last occurrence of "the" between the start and index 8 is at index 6.
//      The last occurrence of "the" between index 5 and index 10 is at index 10.
// Creates and initializes a new Array with three elements of the same value.
Array myArray=Array.CreateInstance( typeof(string), 12 );
myArray.SetValue( "the", 0 );
myArray.SetValue( "quick", 1 );
myArray.SetValue( "brown", 2 );
myArray.SetValue( "fox", 3 );
myArray.SetValue( "jumps", 4 );
myArray.SetValue( "over", 5 );
myArray.SetValue( "the", 6 );
myArray.SetValue( "lazy", 7 );
myArray.SetValue( "dog", 8 );
myArray.SetValue( "in", 9 );
myArray.SetValue( "the", 10 );
myArray.SetValue( "barn", 11 );

// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintIndexAndValues( myArray );

// Searches for the last occurrence of the duplicated value.
string myString = "the";
int myIndex = Array.LastIndexOf( myArray, myString );
Console.WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in the first section of the Array.
myIndex = Array.LastIndexOf( myArray, myString, 8 );
Console.WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex );

// Searches for the last occurrence of the duplicated value in a section of the Array.
// Note that the start index is greater than the end index because the search is done backward.
myIndex = Array.LastIndexOf( myArray, myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 5 and index 10 is at index {1}.", myString, myIndex );

void PrintIndexAndValues( Array anArray )  {
   for ( int i = anArray.GetLowerBound(0); i <= anArray.GetUpperBound(0); i++ )
      Console.WriteLine( "\t[{0}]:\t{1}", i, anArray.GetValue( i ) );
}

/*
This code produces 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 last occurrence of "the" is at index 10.
The last occurrence of "the" between the start and index 8 is at index 6.
The last occurrence of "the" between index 5 and index 10 is at index 10.
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array with three elements of
        ' the same value.
        Dim myArray As Array = Array.CreateInstance(GetType(String), 12)
        myArray.SetValue("the", 0)
        myArray.SetValue("quick", 1)
        myArray.SetValue("brown", 2)
        myArray.SetValue("fox", 3)
        myArray.SetValue("jumps", 4)
        myArray.SetValue("over", 5)
        myArray.SetValue("the", 6)
        myArray.SetValue("lazy", 7)
        myArray.SetValue("dog", 8)
        myArray.SetValue("in", 9)
        myArray.SetValue("the", 10)
        myArray.SetValue("barn", 11)
        
        ' Displays the values of the Array.
        Console.WriteLine("The Array contains the following values:")
        PrintIndexAndValues(myArray)
        
        ' Searches for the last occurrence of the duplicated value.
        Dim myString As String = "the"
        Dim myIndex As Integer = Array.LastIndexOf(myArray, myString)
        Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", _
           myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in the first
        ' section of the Array.
        myIndex = Array.LastIndexOf(myArray, myString, 8)
        Console.WriteLine("The last occurrence of ""{0}"" between the start " _
           + "and index 8 is at index {1}.", myString, myIndex)
        
        ' Searches for the last occurrence of the duplicated value in a section
        ' of the Array.  Note that the start index is greater than the end
        ' index because the search is done backward.
        myIndex = Array.LastIndexOf(myArray, myString, 10, 6)
        Console.WriteLine("The last occurrence of ""{0}"" between index 5 " _
           + "and index 10 is at index {1}.", myString, myIndex)
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' This code produces 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 last occurrence of "the" is at index 10.
' The last occurrence of "the" between the start and index 8 is at index 6.
' The last occurrence of "the" between index 5 and index 10 is at index 10.

注釈

が 0 より大きい場合count、1 次元Arrayは から始まりstartIndex、マイナス count 1 でstartIndex終わる後方に検索されます。

要素は、 メソッドを使用して指定された値と Object.Equals 比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、Equals その型の実装が使用されます。

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

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

.NET Framework 2.0 以降のバージョンでは、このメソッドは の メソッドと CompareTo メソッドを使用Equalsして、 パラメーターでvalue指定された がObject存在Arrayするかどうかを判断します。 以前のバージョンの.NET Frameworkでは、この決定は それ自体の Equals メソッドと CompareTo メソッドをvalueObject使用して行われました。

こちらもご覧ください

適用対象

LastIndexOf<T>(T[], T)

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

指定したオブジェクトを Array 全体を対象に検索し、インデックス番号の最も大きい要素のインデックスを返します。

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

型パラメーター

T

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の Array。インデックス番号は 0 から始まります。

value
T

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

戻り値

value が見つかった場合は、array 全体で最後に見つかった位置のインデックス番号 (0 から始まる)。それ以外の場合は -1。

例外

arraynullです。

次のコード例では、 メソッドの 3 つのジェネリック オーバーロードすべてを示します LastIndexOf 。 文字列の配列が作成され、1 つのエントリがインデックス位置 0 とインデックス位置 5 に 2 回表示されます。 メソッドオーバーロードは LastIndexOf<T>(T[], T) 、配列全体を最後から検索し、2 番目に出現する文字列を検索します。 メソッドオーバーロードは LastIndexOf<T>(T[], T, Int32) 、インデックス位置 3 で始まり、配列の先頭まで続けて配列を後方に検索し、文字列の最初の出現箇所を検索するために使用されます。 最後に、メソッド オーバーロードは、 LastIndexOf<T>(T[], T, Int32, Int32) インデックス位置 4 から始まり、後方に拡張する (つまり、場所 4、3、2、1 の項目を検索する) 4 つのエントリの範囲を検索するために使用されます。この検索では、その範囲に検索文字列のインスタンスがないため、-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.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
 */
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System

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

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

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

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

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

// This code example produces the following output:
//    
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -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.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))

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

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

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1

注釈

Arrayは、最後の要素から始まり、最初の要素で終わる後方に検索されます。

要素は、 メソッドを使用して指定された値と Object.Equals 比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、 Equals その型の実装が使用されます。

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

こちらもご覧ください

適用対象

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

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

指定したオブジェクトを、Array の先頭の要素から、指定されたインデックス位置までを範囲として検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

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

型パラメーター

T

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の Array。インデックス番号は 0 から始まります。

value
T

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

startIndex
Int32

後方検索の開始位置を示す 0 から始まるインデックス。

戻り値

最初の要素から startIndex までの array 内の要素の範囲内で value が見つかった場合は、最後に見つかった位置の 0 から始まるインデックス番号。それ以外の場合は -1。

例外

arraynullです。

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

次のコード例では、 メソッドの 3 つのジェネリック オーバーロードすべてを示します LastIndexOf 。 文字列の配列が作成され、1 つのエントリがインデックス位置 0 とインデックス位置 5 に 2 回表示されます。 メソッドオーバーロードは LastIndexOf<T>(T[], T) 、配列全体を最後から検索し、2 番目に出現する文字列を検索します。 メソッドオーバーロードは LastIndexOf<T>(T[], T, Int32) 、インデックス位置 3 で始まり、配列の先頭まで続けて配列を後方に検索し、文字列の最初の出現箇所を検索するために使用されます。 最後に、メソッド オーバーロードは、 LastIndexOf<T>(T[], T, Int32, Int32) インデックス位置 4 から始まり、後方に拡張する (つまり、場所 4、3、2、1 の項目を検索する) 4 つのエントリの範囲を検索するために使用されます。この検索では、その範囲に検索文字列のインスタンスがないため、-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.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
 */
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System

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

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

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

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

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

// This code example produces the following output:
//    
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -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.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))

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

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

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1

注釈

Array 、最初の要素から startIndex 始まり、最後まで後方に検索されます。

要素は、 メソッドを使用して Object.Equals 指定された値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、 Equals その型の実装が使用されます。

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

こちらもご覧ください

適用対象

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

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

指定したオブジェクトを、指定した数の要素を含み、かつ指定したインデックスで終了する Array の要素の範囲内で検索し、インデックス番号の最も大きい要素のインデックス番号を返します。

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

型パラメーター

T

配列要素の型。

パラメーター

array
T[]

検索する 1 次元の Array。インデックス番号は 0 から始まります。

value
T

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

startIndex
Int32

後方検索の開始位置を示す 0 から始まるインデックス。

count
Int32

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

戻り値

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

例外

arraynullです。

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

または

count が 0 未満です。

または

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

次のコード例では、 メソッドの 3 つのジェネリック オーバーロード LastIndexOf すべてを示します。 文字列の配列が作成され、インデックス位置 0 とインデックス位置 5 に 1 つのエントリが 2 回表示されます。 メソッド オーバーロードは LastIndexOf<T>(T[], T) 、配列全体を最後から検索し、文字列の 2 番目の出現箇所を検索します。 メソッドオーバーロードは LastIndexOf<T>(T[], T, Int32) 、インデックス位置 3 から始まり、配列の先頭まで続く配列を後方に検索するために使用され、文字列の最初の出現箇所を検索します。 最後に、 LastIndexOf<T>(T[], T, Int32, Int32) メソッド オーバーロードを使用して、インデックス位置 4 から後方に拡張する 4 つのエントリの範囲を検索します (つまり、場所 4、3、2、1 の項目を検索します)。この検索では、その範囲内に検索文字列のインスタンスがないため、-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.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus"));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

    Console::WriteLine(
        "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}", 
        Array::LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
 */
string[] dinosaurs = { "Tyrannosaurus",
    "Amargasaurus",
    "Mamenchisaurus",
    "Brachiosaurus",
    "Deinonychus",
    "Tyrannosaurus",
    "Compsognathus" };

Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
    Console.WriteLine(dinosaur);
}

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\"): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 3): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3));

Console.WriteLine(
    "\nArray.LastIndexOf(dinosaurs, \"Tyrannosaurus\", 4, 4): {0}",
    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4));

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Deinonychus
Tyrannosaurus
Compsognathus

Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0

Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1
*/
open System

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

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

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

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

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

// This code example produces the following output:
//    
//    Tyrannosaurus
//    Amargasaurus
//    Mamenchisaurus
//    Brachiosaurus
//    Deinonychus
//    Tyrannosaurus
//    Compsognathus
//    
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
//
//    Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -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.LastIndexOf(dinosaurs, ""Tyrannosaurus""): {0}", _
            Array.LastIndexOf(dinosaurs, "Tyrannosaurus"))

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

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

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Deinonychus
'Tyrannosaurus
'Compsognathus
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus"): 5
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 3): 0
'
'Array.LastIndexOf(dinosaurs, "Tyrannosaurus", 4, 4): -1

注釈

Arrayが 0 より大きい場合countは、 からstartIndex逆方向に検索され、マイナス count 1 で終わるstartIndex

要素は、 メソッドを使用して Object.Equals 指定された値と比較されます。 要素型が非intrinsic (ユーザー定義) 型の場合は、 Equals その型の実装が使用されます。

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

こちらもご覧ください

適用対象