using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Example9_14
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList myArrayList = new ArrayList(3);            myArrayList.Add("Hello 1");
            myArrayList.Add("World 2");            //分别在1,2,插入Jack&this is the
            myArrayList.Insert(1, "Jack 1");
            myArrayList.Insert(2, "this is the 2");            Console.WriteLine("Capacity{0}:", myArrayList.Capacity);
            
                                               //为什么会输出的Capacity的值是6呢?
            
            Console.WriteLine("Count{0}", myArrayList.Count);            Console.WriteLine(myArrayList[0]);
            Console.WriteLine(myArrayList[1]);
            Console.WriteLine(myArrayList[2]);
            Console.WriteLine(myArrayList[3]);            Console.ReadLine();
        }
    }
}

解决方案 »

  1.   

    获取或设置 ArrayList 可包含的元素数。
      

  2.   

    反正这个Capacity要比Count大  大多少 不一定
      

  3.   

    Count的值是4,这个我能理解到,因为我插入的是4个值,但是我不能理解为什么capacity为什么会是6,我觉得应该是7,因为初始的时候是3,然后我插入4个值,莫非C#中0不作为一个数字计算么?
    谢谢您!~
      

  4.   

    myArrayList.Capacity是指arraylist中最多可以放多少元素,如果实际数量大于这个数的话,Capacity就会乘2,来扩充自己。myArrayList.Count是list中的元素实际数量。
      

  5.   

    tjvictor
    谢谢您!
    我懂了哈!~
      

  6.   

    Capacity 是 ArrayList 可存储的元素数。Count 是 ArrayList 中实际存储的元素数。Capacity 总是大于或等于 Count。如果在添加元素时 Count 超过 Capacity,则通过在复制旧元素和添加新元素之前重新分配内部数组来使容量自动增加。