This documentation is archived and is not being maintained.

ArrayList.GetRange Method

Returns an ArrayList which represents a subset of the elements in the source ArrayList.

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

Parameters

index
The zero-based ArrayList index at which the range starts.
count
The number of elements in the range.

Return Value

An ArrayList which represents a subset of the elements in the source ArrayList.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException index is less than zero.

-or-

count is less than zero.

ArgumentException index and count do not denote a valid range of elements in the ArrayList.

Remarks

This method does not create copies of the elements. The new ArrayList is only a view window into the source ArrayList. However, all subsequent changes to the source ArrayList must be done through this view window ArrayList. If changes are made directly to the source ArrayList, the view window ArrayList is invalidated and any operations on it will return an InvalidOperationException.

Example

The following example shows how to set and get a range of elements in the ArrayList.

[Visual Basic] 
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
 */ 

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

ArrayList Class | ArrayList Members | System.Collections Namespace | RemoveRange | AddRange | InsertRange | SetRange

Show: