次の方法で共有


ArrayList.GetRange メソッド

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

Public Overridable Function GetRange( _
   ByVal index As Integer, _   ByVal count As Integer _) As ArrayList
[C#]
public virtual ArrayList GetRange(intindex,intcount);
[C++]
public: virtual ArrayList* GetRange(intindex,intcount);
[JScript]
public function GetRange(
   index : int,count : int) : ArrayList;

パラメータ

  • index
    範囲が開始する位置の、0 から始まる ArrayList のインデックス番号。
  • count
    範囲内の要素の数。

戻り値

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

例外

例外の種類 条件
ArgumentOutOfRangeException index が 0 未満です。

または

count が 0 未満です。

ArgumentException index および count が ArrayList 内の要素の有効範囲を示していません。

解説

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

使用例

ArrayList 内の要素の範囲を設定および取得する方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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("jumped")
        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, ControlChars.Tab)
        
        ' 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, ControlChars.Tab)
    End Sub    
    
    Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

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

[C#] 
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( "jumped" );
      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 )  {
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Index 0 through 4 contains:
    The    quick    brown    fox    jumped
Index 0 through 4 now contains:
    The    big    gray    wolf    jumped
*/ 

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

void PrintValues( IEnumerable* myList, String* mySeparator );
 
int main()  {
 
       // Creates and initializes a new ArrayList.
       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" );
 
       // Creates and initializes the source ICollection.
       Queue* mySourceList = new Queue();
       mySourceList->Enqueue( S"big" );
       mySourceList->Enqueue( S"gray" );
       mySourceList->Enqueue( S"wolf" );
 
       String* szSeparator = "\t";
       // 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, szSeparator );
 
       // 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, szSeparator );
    }
 
void PrintValues( IEnumerable* myList, String* mySeparator )  {
       System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
       while ( myEnumerator->MoveNext() )
          Console::Write( "{0}{1}", mySeparator, myEnumerator->Current );
       Console::WriteLine();
    }

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

 */ 

[JScript] 
import System;
import System.Collections;

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

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

// Displays the values of five elements starting at index 0.
var mySubAL : ArrayList  = 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' );
 
function PrintValues( myList : IEnumerable, mySeparator : char  )  {
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
   Console.WriteLine();
}
 /* 
 This code produces the following output.
 
 Index 0 through 4 contains:
     The    quick    brown    fox    jumped
 Index 0 through 4 now contains:
     The    big    gray    wolf    jumped
 */ 

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

ArrayList クラス | ArrayList メンバ | System.Collections 名前空間 | RemoveRange | AddRange | InsertRange | SetRange