Freigeben über


ArrayList.InsertRange-Methode

Fügt die Elemente einer Auflistung am angegebenen Index in die ArrayList ein.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable Sub InsertRange ( _
    index As Integer, _
    c As ICollection _
)
'Usage
Dim instance As ArrayList
Dim index As Integer
Dim c As ICollection

instance.InsertRange(index, c)
public virtual void InsertRange (
    int index,
    ICollection c
)
public:
virtual void InsertRange (
    int index, 
    ICollection^ c
)
public void InsertRange (
    int index, 
    ICollection c
)
public function InsertRange (
    index : int, 
    c : ICollection
)

Parameter

  • index
    Der nullbasierte Index, an dem neue Elemente eingefügt werden sollen.
  • c
    Die ICollection, deren Elemente in die ArrayList eingefügt werden sollen. Die Auflistung selbst kann nicht NULL (Nothing in Visual Basic) sein, aber sie kann Elemente enthalten, die NULL (Nothing in Visual Basic) sind.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

c ist NULL (Nothing in Visual Basic).

ArgumentOutOfRangeException

index ist kleiner als 0 (null).

– oder –

index ist größer als Count.

NotSupportedException

ArrayList ist schreibgeschützt.

– oder –

ArrayList hat eine feste Größe.

Hinweise

ArrayList akzeptiert NULL (Nothing in Visual Basic) als gültigen Wert und lässt doppelte Elemente zu.

Wenn der neue Count (der aktuelle Count plus die Größe der Auflistung) größer als Capacity ist, wird die Kapazität von ArrayList automatisch durch Neureservieren des internen Arrays erhöht, um die neuen Elemente aufnehmen zu können, und die vorhandenen Elemente werden vor dem Hinzufügen der neuen Elemente in das neue Array kopiert.

Wenn index gleich Count ist, werden die Elemente am Ende der ArrayList hinzugefügt.

Die Reihenfolge der Elemente in der ICollection wird in der ArrayList beibehalten.

In Auflistungen mit zusammenhängenden Elementen, z. B. Listen, werden die Elemente hinter der Einfügemarke nach unten verschoben, um das neue Element aufzunehmen. Wenn die Auflistung indiziert ist, werden auch die Indizes der verschobenen Elemente aktualisiert. Dies gilt nicht für Auflistungen, in denen die Elemente konzeptionell in Buckets gruppiert sind, z. B. Hashtabellen.

Diese Methode ist eine O(n + m)-Operation, wobei n die Anzahl der hinzuzufügenden Elemente und mCount ist.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie Elemente in die ArrayList eingefügt werden.

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 obj As [Object]
        For Each obj In  myList
            Console.Write("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

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()
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 )  {
      foreach ( Object obj in myList )
         Console.Write( "   {0}", obj );
      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()
*/
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 = gcnew 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 = gcnew 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: {0}", myException );
   }

}

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

   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()
 */
import System.*;
import System.Collections.*;

public class SamplesArrayList
{
    public static void main(String[] args)
    {
        // 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.get_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.get_Count() + 1, "anystring");
        }
        catch (System.Exception myException) {
            Console.WriteLine("Exception: " + myException.ToString());
        }
    } //main

    public static void PrintValues(IEnumerable myList)
    {
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("   {0}", obj);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArrayList 
/* 
 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(String[] args)
 */
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()
 */ 

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ArrayList-Klasse
ArrayList-Member
System.Collections-Namespace
Insert
AddRange
SetRange
GetRange
RemoveRange