ArrayList.GetRange(Int32, Int32) メソッド

定義

元の ArrayList 内の要素のサブセットを表す ArrayList を返します。

public:
 virtual System::Collections::ArrayList ^ GetRange(int index, int count);
public virtual System.Collections.ArrayList GetRange (int index, int count);
abstract member GetRange : int * int -> System.Collections.ArrayList
override this.GetRange : int * int -> System.Collections.ArrayList
Public Overridable Function GetRange (index As Integer, count As Integer) As ArrayList

パラメーター

index
Int32

範囲が開始する位置の、0 から始まる ArrayList のインデックス番号。

count
Int32

範囲内の要素の数。

戻り値

元の ArrayList 内の要素のサブセットを表す ArrayList

例外

index が 0 未満です。

または

count が 0 未満です。

index および countArrayList において要素の有効な範囲を表していません。

次のコード例は、 内の要素の範囲を設定および取得する方法を ArrayList示しています。

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList, char mySeparator );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew 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" );
   
   // Creates and initializes the source ICollection.
   Queue^ mySourceList = gcnew Queue;
   mySourceList->Enqueue( "big" );
   mySourceList->Enqueue( "gray" );
   mySourceList->Enqueue( "wolf" );
   
   // Displays the values of five elements starting at index 0.
   ArrayList^ mySubAL = myAL->GetRange( 0, 5 );
   Console::WriteLine( "Index 0 through 4 contains:" );
   PrintValues( mySubAL, '\t' );
   
   // Replaces the values of five elements starting at index 1 with the values in the ICollection.
   myAL->SetRange( 1, mySourceList );
   
   // Displays the values of five elements starting at index 0.
   mySubAL = myAL->GetRange( 0, 5 );
   Console::WriteLine( "Index 0 through 4 now contains:" );
   PrintValues( mySubAL, '\t' );
}

void PrintValues( IEnumerable^ myList, char mySeparator )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "{0}{1}", mySeparator, obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 Index 0 through 4 contains:
         The     quick   brown   fox     jumps
 Index 0 through 4 now contains:
         The     big     gray    wolf    jumps
 */
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      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" );

      // Creates and initializes the source ICollection.
      Queue mySourceList = new Queue();
      mySourceList.Enqueue( "big" );
      mySourceList.Enqueue( "gray" );
      mySourceList.Enqueue( "wolf" );

      // Displays the values of five elements starting at index 0.
      ArrayList mySubAL = myAL.GetRange( 0, 5 );
      Console.WriteLine( "Index 0 through 4 contains:" );
      PrintValues( mySubAL, '\t' );

      // Replaces the values of five elements starting at index 1 with the values in the ICollection.
      myAL.SetRange( 1, mySourceList );

      // Displays the values of five elements starting at index 0.
      mySubAL = myAL.GetRange( 0, 5 );
      Console.WriteLine( "Index 0 through 4 now contains:" );
      PrintValues( mySubAL, '\t' );
   }

   public static void PrintValues( IEnumerable myList, char mySeparator )  {
      foreach ( Object obj in myList )
         Console.Write( "{0}{1}", mySeparator, obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

Index 0 through 4 contains:
        The     quick   brown   fox     jumps
Index 0 through 4 now contains:
        The     big     gray    wolf    jumps
*/
Imports System.Collections

Public Class SamplesArrayList

    Public Shared Sub Main()

        ' Creates and initializes a new ArrayList.
        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")

        ' Creates and initializes the source ICollection.
        Dim mySourceList As New Queue()
        mySourceList.Enqueue("big")
        mySourceList.Enqueue("gray")
        mySourceList.Enqueue("wolf")

        ' Displays the values of five elements starting at index 0.
        Dim mySubAL As ArrayList = myAL.GetRange(0, 5)
        Console.WriteLine("Index 0 through 4 contains:")
        PrintValues(mySubAL, vbTab)

        ' Replaces the values of five elements starting at index 1 with the values in the ICollection.
        myAL.SetRange(1, mySourceList)

        ' Displays the values of five elements starting at index 0.
        mySubAL = myAL.GetRange(0, 5)
        Console.WriteLine("Index 0 through 4 now contains:")
        PrintValues(mySubAL, vbTab)

    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("{0}{1}", mySeparator, obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' Index 0 through 4 contains:
'         The     quick   brown   fox     jumps
' Index 0 through 4 now contains:
'         The     big     gray    wolf    jumps

注釈

このメソッドでは、 要素のコピーは作成されません。 新しい ArrayList は、ソース ArrayListへのビュー ウィンドウのみです。 ただし、ソース ArrayList に対するそれ以降のすべての変更は、このビュー ウィンドウ ArrayListで行う必要があります。 ソース ArrayListに直接変更が加えられた場合、ビュー ウィンドウ ArrayList は無効になり、それに対するすべての操作で が InvalidOperationException返されます。

このメソッドは操作です O(1)

適用対象

こちらもご覧ください