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
*/ 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值