Auf Englisch lesen

Teilen über


ArrayList.AddRange(ICollection) Methode

Definition

Fügt die Elemente einer ICollection am Ende der ArrayList hinzu.

C#
public virtual void AddRange (System.Collections.ICollection c);

Parameter

c
ICollection

Die ICollection, deren Elemente am Ende der ArrayList hinzugefügt werden sollen. Die Auflistung selbst kann nicht null sein, aber sie kann Elemente enthalten, die null sind.

Ausnahmen

c ist null.

ArrayList ist schreibgeschützt.

- oder -

ArrayList hat eine feste Größe.

Beispiele

Im folgenden Codebeispiel wird gezeigt, wie Elemente zu hinzugefügt werden ArrayList.

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" );

      // Creates and initializes a new Queue.
      Queue myQueue = new Queue();
      myQueue.Enqueue( "jumps" );
      myQueue.Enqueue( "over" );
      myQueue.Enqueue( "the" );
      myQueue.Enqueue( "lazy" );
      myQueue.Enqueue( "dog" );

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

      // Copies the Queue elements to the end of the ArrayList.
      myAL.AddRange( myQueue );

      // Displays the ArrayList.
      Console.WriteLine( "The ArrayList now contains the following:" );
      PrintValues( myAL, '\t' );
   }

   public static void PrintValues( IEnumerable myList, char mySeparator )  {
      foreach ( Object obj in myList )
         Console.Write( "{0}{1}", mySeparator, obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList initially contains the following:
    The    quick    brown    fox
The Queue initially contains the following:
    jumps    over    the    lazy    dog
The ArrayList now contains the following:
    The    quick    brown    fox    jumps    over    the    lazy    dog
*/

Hinweise

ArrayList akzeptiert null als gültigen Wert und lässt doppelte Elemente zu.

Die Reihenfolge der Elemente im ICollection wird im ArrayListbeibehalten.

Wenn das neue Count (aktuelle Count plus die Größe der Auflistung) größer als Capacityist, wird die Kapazität von ArrayList erhöht, indem das interne Array automatisch neu zugeordnet wird, um die neuen Elemente aufzunehmen, und die vorhandenen Elemente werden in das neue Array kopiert, bevor die neuen Elemente hinzugefügt werden.

Wenn die ArrayList die neuen Elemente aufnehmen kann, ohne zu Capacityerhöhen, ist diese Methode ein O(n) Vorgang, wobei n die Anzahl der hinzuzufügenden Elemente ist. Wenn die Kapazität erhöht werden muss, um die neuen Elemente aufzunehmen, wird diese Methode zu einem O(n + m) Vorgang, wobei n die Anzahl der hinzuzufügenden Elemente und m der Wert ist Count.

Gilt für:

Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Weitere Informationen