英語で読む

次の方法で共有


ArrayList.LastIndexOf メソッド

定義

ArrayList またはその一部で最後に出現した値の 0 から始まるインデックスを返します。

オーバーロード

LastIndexOf(Object)

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

LastIndexOf(Object, Int32)

指定した Object を検索し、最初の要素から指定したインデックスに拡張される ArrayList 内の要素の範囲内で最後に出現した位置の 0 から始まるインデックスを返します。

LastIndexOf(Object, Int32, Int32)

指定した Object を検索し、指定した数の要素を含み、指定したインデックスで終わる ArrayList 内の要素の範囲内で最後に出現した位置の 0 から始まるインデックスを返します。

LastIndexOf(Object)

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

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

C#
public virtual int LastIndexOf (object value);
C#
public virtual int LastIndexOf (object? value);

パラメーター

value
Object

ArrayList内で検索する Object。 値は nullできます。

戻り値

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

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

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 5 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "   [{0}]:    {1}", i++, obj );
      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 5 is at index 10.
*/

注釈

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

このメソッドは線形検索を実行します。したがって、このメソッドは、nCountO(n) 操作です。

.NET Framework 2.0 以降では、このメソッドはコレクションのオブジェクトの Equals メソッドと CompareTo メソッドを使用して、item に項目が存在するかどうかを判断します。 以前のバージョンの .NET Framework では、この決定は、コレクション内のオブジェクトの item パラメーターの Equals メソッドと CompareTo メソッドを使用して行われました。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

LastIndexOf(Object, Int32)

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

指定した Object を検索し、最初の要素から指定したインデックスに拡張される ArrayList 内の要素の範囲内で最後に出現した位置の 0 から始まるインデックスを返します。

C#
public virtual int LastIndexOf (object value, int startIndex);
C#
public virtual int LastIndexOf (object? value, int startIndex);

パラメーター

value
Object

ArrayList内で検索する Object。 値は nullできます。

startIndex
Int32

後方検索の 0 から始まる開始インデックス。

戻り値

最初の要素から startIndexに拡張される ArrayList 内の要素の範囲内で、value が最後に出現した位置の 0 から始まるインデックス (見つかった場合)。それ以外の場合は -1。

例外

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

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

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 5 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "   [{0}]:    {1}", i++, obj );
      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 5 is at index 10.
*/

注釈

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

このメソッドは線形検索を実行します。したがって、このメソッドは O(n) 演算であり、ここで nArrayList の先頭から startIndexまでの要素の数です。

このメソッドは、Object.Equalsを呼び出すことによって等価性を決定します。

.NET Framework 2.0 以降では、このメソッドはコレクションのオブジェクトの Equals メソッドと CompareTo メソッドを使用して、item に項目が存在するかどうかを判断します。 以前のバージョンの .NET Framework では、この決定は、コレクション内のオブジェクトの item パラメーターの Equals メソッドと CompareTo メソッドを使用して行われました。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

LastIndexOf(Object, Int32, Int32)

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

指定した Object を検索し、指定した数の要素を含み、指定したインデックスで終わる ArrayList 内の要素の範囲内で最後に出現した位置の 0 から始まるインデックスを返します。

C#
public virtual int LastIndexOf (object value, int startIndex, int count);
C#
public virtual int LastIndexOf (object? value, int startIndex, int count);

パラメーター

value
Object

ArrayList内で検索する Object。 値は nullできます。

startIndex
Int32

後方検索の 0 から始まる開始インデックス。

count
Int32

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

戻り値

count 数の要素を含み、見つかった場合は startIndexで終わる、ArrayList 内の要素の範囲内で最後に発生した value の 0 から始まるインデックス。それ以外の場合は -1。

例外

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

-又は-

count は 0 未満です。

-又は-

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

次のコード例は、指定した要素の最後に出現するインデックスを決定する方法を示しています。 LastIndexOf は後方検索であることに注意してください。したがって、countstartIndex + 1 以下である必要があります。

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 5 is at index {1}.", myString, myIndex );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "   [{0}]:    {1}", i++, obj );
      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 5 is at index 10.
*/

注釈

ArrayList は、count が 0 より大きい場合は、startIndex から後方に検索され、startIndex - count + 1 で終わる。

このメソッドは線形検索を実行します。したがって、このメソッドは、ncountO(n) 操作です。

このメソッドは、Object.Equalsを呼び出すことによって等価性を決定します。

.NET Framework 2.0 以降では、このメソッドはコレクションのオブジェクトの Equals メソッドと CompareTo メソッドを使用して、item に項目が存在するかどうかを判断します。 以前のバージョンの .NET Framework では、この決定は、コレクション内のオブジェクトの item パラメーターの Equals メソッドと CompareTo メソッドを使用して行われました。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0