ArrayList.AddRange 方法

原文:http://technet.microsoft.com/zh-cn/library/system.collections.arraylist.addrange(v=vs.90)

ArrayList 接受 null 作为有效值并且允许重复的元素。

ICollection 中元素的顺序保存在 ArrayList 中。

如果新的 Count(当前 Count 加上集合的大小)大于 Capacity,则会通过自动重新分配内部数组增大 ArrayList 的容量以容纳新元素,并在添加新元素之前将现有元素复制到新数组中。

如果 ArrayList 可以在不增加 Capacity 的情况下容纳新元素,则此方法是 O(n) 运算,其中 n 是要添加的元素数。如果需要增加此容量以容纳新元素,则此方法变为 O(n + m) 运算,其中 n 是要添加的元素数,m 是 Count

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( "jumped" );
      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:
    jumped    over    the    lazy    dog
The ArrayList now contains the following:
    The    quick    brown    fox    jumped    over    the    lazy    dog
*/ 


你可能感兴趣的:(ArrayList,AddRange)