ArrayList.AddRange(ICollection) Méthode

Définition

Ajoute les éléments de ICollection à la fin de ArrayList.

public:
 virtual void AddRange(System::Collections::ICollection ^ c);
public virtual void AddRange (System.Collections.ICollection c);
abstract member AddRange : System.Collections.ICollection -> unit
override this.AddRange : System.Collections.ICollection -> unit
Public Overridable Sub AddRange (c As ICollection)

Paramètres

c
ICollection

ICollection dont les éléments doivent être ajoutés à la fin de ArrayList. La collection elle-même ne peut pas avoir la valeur null, mais elle peut contenir des éléments qui ont la valeur null.

Exceptions

c a la valeur null.

ArrayList est en lecture seule.

- ou -

ArrayList est de taille fixe.

Exemples

L’exemple de code suivant montre comment ajouter des éléments au ArrayList.

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList, char mySeparator );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "The" );
   myAL->Add( "quick" );
   myAL->Add( "brown" );
   myAL->Add( "fox" );
   
   // Creates and initializes a new Queue.
   Queue^ myQueue = gcnew 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' );
}

void PrintValues( IEnumerable^ myList, char mySeparator )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      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
*/
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
*/
Imports System.Collections

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")
        
        ' Creates and initializes a new Queue.
        Dim myQueue As 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, ControlChars.Tab)
        Console.WriteLine("The Queue initially contains the following:")
        PrintValues(myQueue, ControlChars.Tab)
        
        ' 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, ControlChars.Tab)
    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
        Dim obj As [Object]
        For Each obj In  myList
          Console.Write( "{0}{1}", mySeparator, obj )
        Next obj
        Console.WriteLine()
    End Sub

End Class


' 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

Remarques

ArrayList accepte null comme valeur valide et autorise les éléments en double.

L’ordre des éléments dans le ICollection est conservé dans .ArrayList

Si le nouveau Count (l’actuel Count plus la taille de la collection) est supérieur Capacityà , la capacité du ArrayList est augmentée en réaffectant automatiquement le tableau interne pour prendre en charge les nouveaux éléments, et les éléments existants sont copiés dans le nouveau tableau avant l’ajout des nouveaux éléments.

Si le ArrayList peut prendre en charge les nouveaux éléments sans augmenter le Capacity, cette méthode est une O(n) opération, où n est le nombre d’éléments à ajouter. Si la capacité doit être augmentée pour prendre en charge les nouveaux éléments, cette méthode devient une O(n + m) opération, où n est le nombre d’éléments à ajouter et m est Count.

S’applique à

Voir aussi