次の方法で共有


ArrayList.LastIndexOf メソッド

ArrayList 内またはその一部にある値のうち、最後に出現する値の、0 から始まるインデックス番号を返します。

オーバーロードの一覧

指定した Object を検索し、 ArrayList 全体内で最後に見つかった位置の 0 から始まるインデックスを返します。

[Visual Basic] Overloads Public Overridable Function LastIndexOf(Object) As Integer

[C#] public virtual int LastIndexOf(object);

[C++] public: virtual int LastIndexOf(Object*);

[JScript] public function LastIndexOf(Object) : int;

指定した Object を検索し、最初の要素から、指定したインデックスまでの ArrayList のセクション内で最後に出現する位置の 0 から始まるインデックス番号を返します。

[Visual Basic] Overloads Public Overridable Function LastIndexOf(Object, Integer) As Integer

[C#] public virtual int LastIndexOf(object, int);

[C++] public: virtual int LastIndexOf(Object*, int);

[JScript] public function LastIndexOf(Object, int) : int;

指定した Object を検索して、指定した数の要素を格納し、指定したインデックスの位置で終了する ArrayList のセクション内で最初に出現する位置の 0 から始まるインデックス番号を返します。

[Visual Basic] Overloads Public Overridable Function LastIndexOf(Object, Integer, Integer) As Integer

[C#] public virtual int LastIndexOf(object, int, int);

[C++] public: virtual int LastIndexOf(Object*, int, int);

[JScript] public function LastIndexOf(Object, int, int) : int;

使用例

最後に見つかった指定要素のインデックスを確認する方法を次の例に示します。LastIndexOf は後方検索です。したがって、count は startIndex + 1 以下である必要があります。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList
   
   Public Shared Sub Main()
      
      ' Creates and initializes a new ArrayList with three elements of the same value.
      Dim myAL As New ArrayList()
      myAL.Add("the")
      myAL.Add("quick")
      myAL.Add("brown")
      myAL.Add("fox")
      myAL.Add("jumps")
      myAL.Add("over")
      myAL.Add("the")
      myAL.Add("lazy")
      myAL.Add("dog")
      myAL.Add("in")
      myAL.Add("the")
      myAL.Add("barn")
      
      ' Displays the values of the ArrayList.
      Console.WriteLine("The ArrayList contains the following values:")
      PrintIndexAndValues(myAL)
      
      ' Searches for the last occurrence of the duplicated value.
      Dim myString As [String] = "the"
      Dim myIndex As Integer = myAL.LastIndexOf(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 ArrayList.
      myIndex = myAL.LastIndexOf(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 ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf(myString, 10, 6)
      Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 6 is at index {1}.", myString, myIndex)
   End Sub 'Main
   
   
   Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
      Dim i As Integer = 0
      Dim myEnumerator As System.Collections.IEnumerator = myList.GetEnumerator()
      While myEnumerator.MoveNext()
         Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab + "{1}", i, myEnumerator.Current)
         i = i + 1
      End While
      Console.WriteLine()
   End Sub 'PrintIndexAndValues
End Class 'SamplesArrayList

' This code produces the following output.
'
' The ArrayList 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 10 and index 6 is at index 10.
' 

[C#] 
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList with three elements of the same value.
      ArrayList myAL = new ArrayList();
      myAL.Add( "the" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );
      myAL.Add( "in" );
      myAL.Add( "the" );
      myAL.Add( "barn" );

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

      // Searches for the last occurrence of the duplicated value.
      String myString = "the";
      int myIndex = myAL.LastIndexOf( 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 ArrayList.
      myIndex = myAL.LastIndexOf( 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 ArrayList.  Note that the start index is greater than the end index because the search is done backward.
      myIndex = myAL.LastIndexOf( myString, 10, 6 );
      Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
      Console.WriteLine();
   }
}
/*
This code produces the following output.

The ArrayList 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 10 and index 6 is at index 10.
*/ 

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintIndexAndValues( IEnumerable* myList );
 
int main()  {
 
       // Creates and initializes a new ArrayList instance with three elements of the same value.
       ArrayList* myAL = new ArrayList();
       myAL->Add( S"the" );
       myAL->Add( S"quick" );
       myAL->Add( S"brown" );
       myAL->Add( S"fox" );
       myAL->Add( S"jumped" );
       myAL->Add( S"over" );
       myAL->Add( S"the" );
       myAL->Add( S"lazy" );
       myAL->Add( S"dog" );
       myAL->Add( S"in" );
       myAL->Add( S"the" );
       myAL->Add( S"barn" );
 
       // Displays the values of the ArrayList.
       Console::WriteLine( "The ArrayList instance contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Searches for the last occurrence of the duplicated value.
       String* myString = "the";
       int myIndex = myAL->LastIndexOf( myString );
       Console::WriteLine( "The last occurrence of \"{0}\" is at index {1}.", myString, __box(myIndex) );
 
       // Searches for the last occurrence of the duplicated value in the first section of the ArrayList.
       myIndex = myAL->LastIndexOf( myString, 8 );
       Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, __box(myIndex) );
 
       // Searches for the last occurrence of the duplicated value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.
       myIndex = myAL->LastIndexOf( myString, 10, 6 );
       Console::WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 is at index {1}.", myString, __box(myIndex) );
    }
 
void PrintIndexAndValues( IEnumerable* myList )  {
       int i = 0;
       System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
       while ( myEnumerator->MoveNext() )
          Console::WriteLine( "\t[{0}]:\t{1}", __box(i++), myEnumerator->Current );
       Console::WriteLine();
    }

 /*
 This code produces the following output.
 
 The ArrayList instance contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [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 10 and index 6 is at index 10.
 */ 

[JScript] 
import System;
import System.Collections;

// Creates and initializes a new ArrayList with three elements of the same value.
var myAL : ArrayList = new ArrayList();
myAL.Add( "the" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
myAL.Add( "in" );
myAL.Add( "the" );
myAL.Add( "barn" );

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

// Searches for the last occurrence of the duplicated value.
var myString : String = "the";
var myIndex : int = myAL.LastIndexOf( 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 ArrayList.
myIndex = myAL.LastIndexOf( 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 ArrayList.  Note that the start index is greater than the end index because the search is done backward.
myIndex = myAL.LastIndexOf( myString, 10, 6 );
Console.WriteLine( "The last occurrence of \"{0}\" between index 10 and index 6 is at index {1}.", myString, myIndex );
 
function PrintIndexAndValues( myList : IEnumerable )  {
   var i : int = 0;
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
   Console.WriteLine();
}
 /*
 This code produces the following output.
 
 The ArrayList contains the following values:
     [0]:    the
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [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 10 and index 6 is at index 10.
 */ 

参照

ArrayList クラス | ArrayList メンバ | System.Collections 名前空間