ArrayList.AddRange(ICollection) メソッド

定義

ICollection の要素を 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)

パラメーター

c
ICollection

ArrayList の末尾に要素が追加される ICollection。 コレクション自体を null にすることはできませんが、null である要素を格納することは可能です。

例外

cnullです。

ArrayList は読み取り専用です。

  • または - ArrayList は固定サイズです。

次のコード例は、要素 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

注釈

ArrayListnull 有効な値として受け取り、重複する要素を許可します。

内の要素ICollectionの順序は、ArrayList

新しい要素 (現在Countのコレクションのサイズを加算) が超えるCapacity場合は、新しいCount要素に対応するように内部配列を自動的に再割り当てすることで、その容量ArrayListが増え、新しい要素が追加される前に既存の要素が新しい配列にコピーされます。

ArrayListを増やさずに新しい要素をCapacity格納できる場合、このメソッドはO(n)演算であり、追加nする要素の数です。 新しい要素に対応するために容量を増やす必要がある場合、このメソッドは操作になります O(n + m) 。ここで n 、追加する要素の数は m 次のようになります Count

適用対象

こちらもご覧ください