Export (0) Print
Expand All

ArrayList.InsertRange Method

Inserts the elements of a collection into the ArrayList at the specified index.

[Visual Basic]
Public Overridable Sub InsertRange( _
   ByVal index As Integer, _
   ByVal c As ICollection _
)
[C#]
public virtual void InsertRange(
 int index,
 ICollection c
);
[C++]
public: virtual void InsertRange(
 int index,
 ICollection* c
);
[JScript]
public function InsertRange(
   index : int,
 c : ICollection
);

Parameters

index
The zero-based index at which the new elements should be inserted.
c
The ICollection whose elements should be inserted into 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
ArgumentNullException c is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException index is less than zero.

-or-

index is greater than Count.

NotSupportedException The ArrayList is read-only.

-or-

The ArrayList has a fixed size.

Remarks

ArrayList accepts a null reference (Nothing in Visual Basic) as a valid value and allows duplicate elements.

If the new Count (the current Count plus the size of the collection) is greater than Capacity, the capacity of the list is either doubled or increased to the new count, whichever is greater. The internal array is automatically reallocated to accommodate the new elements.

If index is equal to Count, the elements are added to the end of ArrayList.

The order of the elements in the ICollection is preserved in the ArrayList.

In collections of contiguous elements, such as lists, the elements that follow the insertion point move down to accomodate the new element. If the collection is indexed, the indexes of the elements that are moved are also updated. This behavior does not apply to collections where elements are conceptually grouped into buckets, such as a hashtable.

Example

The following example shows how to insert elements into 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 using Insert instead of Add.
        Dim myAL As New ArrayList()
        myAL.Insert(0, "The")
        myAL.Insert(1, "fox")
        myAL.Insert(2, "jumps")
        myAL.Insert(3, "over")
        myAL.Insert(4, "the")
        myAL.Insert(5, "dog")
        
        ' Creates and initializes a new Queue.
        Dim myQueue As New Queue()
        myQueue.Enqueue("quick")
        myQueue.Enqueue("brown")
        
        ' Displays the ArrayList and the Queue.
        Console.WriteLine("The ArrayList initially contains the following:")
        PrintValues(myAL)
        Console.WriteLine("The Queue initially contains the following:")
        PrintValues(myQueue)
        
        ' Copies the Queue elements to the ArrayList at index 1.
        myAL.InsertRange(1, myQueue)
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding the Queue, the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Search for "dog" and add "lazy" before it.
        myAL.Insert(myAL.IndexOf("dog"), "lazy")
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding ""lazy"", the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Add "!!!" at the end.
        myAL.Insert(myAL.Count, "!!!")
        
        ' Displays the ArrayList.
        Console.WriteLine("After adding ""!!!"", the ArrayList now contains:")
        PrintValues(myAL)
        
        ' Inserting an element beyond Count throws an exception.
        Try
            myAL.Insert(myAL.Count + 1, "anystring")
        Catch myException As Exception
            Console.WriteLine("Exception: " + myException.ToString())
        End Try
    End Sub    
    
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The ArrayList initially contains the following:
'     The    fox    jumps    over    the    dog
' The Queue initially contains the following:
'     quick    brown
' After adding the Queue, the ArrayList now contains:
'     The    quick    brown    fox    jumps    over    the    dog
' After adding "lazy", the ArrayList now contains:
'     The    quick    brown    fox    jumps    over    the    lazy    dog
' After adding "!!!", the ArrayList now contains:
'     The    quick    brown    fox    jumps    over    the    lazy    dog    !!!
' Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
' Parameter name: index
'    at System.Collections.ArrayList.Insert(Int32 index, Object value)
'    at SamplesArrayList.Main()

[C#] 
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList using Insert instead of Add.
      ArrayList myAL = new ArrayList();
      myAL.Insert( 0, "The" );
      myAL.Insert( 1, "fox" );
      myAL.Insert( 2, "jumps" );
      myAL.Insert( 3, "over" );
      myAL.Insert( 4, "the" );
      myAL.Insert( 5, "dog" );

      // Creates and initializes a new Queue.
      Queue myQueue = new Queue();
      myQueue.Enqueue( "quick" );
      myQueue.Enqueue( "brown" );

      // Displays the ArrayList and the Queue.
      Console.WriteLine( "The ArrayList initially contains the following:" );
      PrintValues( myAL );
      Console.WriteLine( "The Queue initially contains the following:" );
      PrintValues( myQueue );

      // Copies the Queue elements to the ArrayList at index 1.
      myAL.InsertRange( 1, myQueue );

      // Displays the ArrayList.
      Console.WriteLine( "After adding the Queue, the ArrayList now contains:" );
      PrintValues( myAL );

      // Search for "dog" and add "lazy" before it.
      myAL.Insert( myAL.IndexOf( "dog" ), "lazy" );

      // Displays the ArrayList.
      Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
      PrintValues( myAL );

      // Add "!!!" at the end.
      myAL.Insert( myAL.Count, "!!!" );

      // Displays the ArrayList.
      Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
      PrintValues( myAL );

      // Inserting an element beyond Count throws an exception.
      try  {
         myAL.Insert( myAL.Count+1, "anystring" );
      } catch ( Exception myException )  {
         Console.WriteLine("Exception: " + myException.ToString());
      }
   }

   public static void PrintValues( IEnumerable myList )  {
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "\t{0}", myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The ArrayList initially contains the following:
        The     fox     jumps  over    the     dog
The Queue initially contains the following:
        quick   brown
After adding the Queue, the ArrayList now contains:
        The     quick   brown   fox     jumps  over    the     dog
After adding "lazy", the ArrayList now contains:
        The     quick   brown   fox     jumps  over    the     lazy    dog
After adding "!!!", the ArrayList now contains:
        The     quick   brown   fox     jumps  over    the     lazy    dog     !!!
Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
Parameter name: index
   at System.Collections.ArrayList.Insert(Int32 index, Object value)
   at SamplesArrayList.Main()
*/ 

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

void PrintValues( IEnumerable* myList );
 
int main()  {
 
       // Creates and initializes a new ArrayList using Insert instead of Add.
       ArrayList* myAL = new ArrayList();
       myAL->Insert( 0, S"The" );
       myAL->Insert( 1, S"fox" );
       myAL->Insert( 2, S"jumped" );
       myAL->Insert( 3, S"over" );
       myAL->Insert( 4, S"the" );
       myAL->Insert( 5, S"dog" );
 
       // Creates and initializes a new Queue.
       Queue* myQueue = new Queue();
       myQueue->Enqueue( S"quick" );
       myQueue->Enqueue( S"brown" );
 
       // Displays the ArrayList and the Queue.
       Console::WriteLine( "The ArrayList initially contains the following:" );
       PrintValues( myAL );
       Console::WriteLine( "The Queue initially contains the following:" );
       PrintValues( myQueue );
 
       // Copies the Queue elements to the ArrayList at index 1.
       myAL->InsertRange( 1, myQueue );
 
       // Displays the ArrayList.
       Console::WriteLine( "After adding the Queue, the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Searches for "dog" and add "lazy" before it.
       myAL->Insert( myAL->IndexOf( S"dog" ), S"lazy" );
 
       // Displays the ArrayList.
       Console::WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Adds "!!!" at the end.
       myAL->Insert( myAL->Count, S"!!!" );
 
       // Displays the ArrayList.
       Console::WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
       PrintValues( myAL );
 
       // Inserting an element beyond Count throws an exception.
       try  {
          myAL->Insert( myAL->Count+1, S"anystring" );
       } catch ( Exception* myException )  {
          Console::WriteLine(String::Concat("Exception: ", myException->ToString()));
       }
    }
 
void PrintValues( IEnumerable* myList )  {
       System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
       while ( myEnumerator->MoveNext() )
          Console::Write( "\t{0}", myEnumerator->Current );
       Console::WriteLine();
    }

 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
         The     fox     jumped  over    the     dog
 The Queue initially contains the following:
         quick   brown
 After adding the Queue, the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     dog
 After adding "lazy", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog
 After adding "!!!", the ArrayList now contains:
         The     quick   brown   fox     jumped  over    the     lazy    dog     !!!
 Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
 Parameter name: index
    at System.Collections.ArrayList.Insert(Int32 index, Object value)
    at SamplesArrayList.Main()
 */ 

[JScript] 
import System;
import System.Collections;


// Creates and initializes a new ArrayList using Insert instead of Add.
var myAL : ArrayList = new ArrayList();
myAL.Insert( 0, "The" );
myAL.Insert( 1, "fox" );
myAL.Insert( 2, "jumped" );
myAL.Insert( 3, "over" );
myAL.Insert( 4, "the" );
myAL.Insert( 5, "dog" );

// Creates and initializes a new Queue.
var myQueue : Queue = new Queue();
myQueue.Enqueue( "quick" );
myQueue.Enqueue( "brown" );

// Displays the ArrayList and the Queue.
Console.WriteLine( "The ArrayList initially contains the following:" );
PrintValues( myAL );
Console.WriteLine( "The Queue initially contains the following:" );
PrintValues( myQueue );

// Copies the Queue elements to the ArrayList at index 1.
myAL.InsertRange( 1, myQueue );

// Displays the ArrayList.
Console.WriteLine( "After adding the Queue, the ArrayList now contains:" );
PrintValues( myAL );

// Search for "dog" and add "lazy" before it.
myAL.Insert( myAL.IndexOf( "dog" ), "lazy" );

// Displays the ArrayList.
Console.WriteLine( "After adding \"lazy\", the ArrayList now contains:" );
PrintValues( myAL );

// Add "!!!" at the end.
myAL.Insert( myAL.Count, "!!!" );

// Displays the ArrayList.
Console.WriteLine( "After adding \"!!!\", the ArrayList now contains:" );
PrintValues( myAL );

// Inserting an element beyond Count throws an exception.
try  {
  myAL.Insert( myAL.Count+1, "anystring" );
} catch ( myException : Exception )  {
  Console.WriteLine("Exception: " + myException.ToString());
}


 
function PrintValues( myList : IEnumerable)  {
   var  myEnumerator : System.Collections.IEnumerator = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.Write( "\t{0}", myEnumerator.Current );
   Console.WriteLine();
}
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    fox    jumped    over    the    dog
 The Queue initially contains the following:
     quick    brown
 After adding the Queue, the ArrayList now contains:
     The    quick    brown    fox    jumped    over    the    dog
 After adding "lazy", the ArrayList now contains:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 After adding "!!!", the ArrayList now contains:
     The    quick    brown    fox    jumped    over    the    lazy    dog    !!!
 Exception: System.ArgumentOutOfRangeException: Insertion index was out of range.  Must be non-negative and less than or equal to size.
 Parameter name: index
    at System.Collections.ArrayList.Insert(Int32 index, Object value)
    at JScript 0.Global Code()
 */ 

Requirements

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

See Also

ArrayList Class | ArrayList Members | System.Collections Namespace | Insert | AddRange | SetRange | GetRange | RemoveRange

Show:
© 2014 Microsoft