This documentation is archived and is not being maintained.
ArrayList.SetRange Method
.NET Framework 1.1
Copies the elements of a collection over a range of elements in the ArrayList.
[Visual Basic] Public Overridable Sub SetRange( _ ByVal index As Integer, _ ByVal c As ICollection _ ) [C#] public virtual void SetRange( int index, ICollection c ); [C++] public: virtual void SetRange( int index, ICollection* c ); [JScript] public function SetRange( index : int, c : ICollection );
Parameters
- index
- The zero-based ArrayList index at which to start copying the elements of c.
- c
- The ICollection whose elements to copy to the ArrayList. The collection itself cannot be a null reference (Nothing in Visual Basic), but it can contain elements that are a null reference (Nothing).
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentOutOfRangeException | index is less than zero.
-or- index plus the number of elements in c is greater than Count. |
| ArgumentNullException | c is a null reference (Nothing in Visual Basic). |
| NotSupportedException | The ArrayList is read-only. |
Remarks
ArrayList accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.
The order of the elements in the ICollection is preserved in the ArrayList.
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 | AddRange | InsertRange | GetRange | RemoveRange
Show: