1

I am learning the properties of Arraylist in C#. I went through the code, and implemented a simple arraylist. Below is the code which I tried.

ArrayList newal= new ArrayList();
newal.Add( "Alice");
newal.Add( "Wonderland");
newal.Add( "Dreamland");
Console.WriteLine( "Capacity:{0}",newal.Capacity );

Here the answer comes as count =3 (if i calculate), which i understand, but the capacity comes as 4. How does it calculate it as 4 ? Does it include null by default ?

I do not know, by the end is it calculating something ?

John Woo
  • 258,903
  • 69
  • 498
  • 492
user2387900
  • 215
  • 1
  • 6
  • 13
  • 1
    It's better to use strongly-typed generic collections like `List` instead of `ArrayList` – Sergey Berezovskiy Aug 04 '13 at 20:15
  • Capacity is the number of elements that the ArrayList can store. Count is the number of elements that are actually in the ArrayList. – Mario S Aug 04 '13 at 20:15
  • 1
    Just to help you understand, it appears that they are implemented as dynamic arrays (https://en.wikipedia.org/wiki/Dynamic_array). – Matt Bryant Aug 04 '13 at 20:17
  • Addressed in [this answer](http://stackoverflow.com/questions/2247773/is-it-worthwhile-to-initialize-the-collection-size-of-a-listt-if-its-size-rea/2248188#2248188). Also the class you should be learning about, ArrayList has no remaining usefulness. – Hans Passant Aug 04 '13 at 20:23

4 Answers4

3

Capacity and Count represent two different things

Count returns the number of items

Capacity tells the maximum number of items ArrayList can currently hold. Capacity will increase automatically when you will put more items in the ArrayList

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • how is the capacity decided ? When it would increase ? Why it is 4 here, the elements are 3 so ?? 3+1 ? If i would have added 2, then capacity would be 3 ? – user2387900 Aug 04 '13 at 20:17
  • 2
    @user2387900 No, it's not like that (3 + 1 = 4). When you create ArrayList without specifying any capacity .Net framework creates one with capacity of 4. When you will insert a 5th item, it will double the capacity and so on... – Haris Hasan Aug 04 '13 at 20:19
  • @HarisHasan When we create without specifying capacity it will be **zero** when adding first element It will make it 4 – Sriram Sakthivel Aug 04 '13 at 20:31
3

Count property tells how many elements are currently in ArrayList where as Capacity property tells how many elements can fit into ArrayList without allocating more memory.

How does it calculate it as 4 ?

When you add an element to List it will check the Capacity whether the element can fit or not. If not it will just "Pre-Allocate" the ArrayList capacity to double of its current Capacity.

So in your example It is 4 since when first element is added Initial Capacity will be set to 4. You can test this by adding more elements to it. After adding 5 elements List Capacity will be 8 and so on.

Hope this helps

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

The Capacity is not the same as the Count. The former is the size of the internal backing array, so how many items can this array hold until it needs to be recreated. The latter is just the count of current items.

A doubling algorithm increases the size of the internal array if required:

EnsureCapacity checks this on ArrayList.Add, so it's 4 at the minimum (ILSpy, .NET 4)

// System.Collections.ArrayList
private void EnsureCapacity(int min)
{
    if (this._items.Length < min)
    {
        int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
        if (num < min)
        {
            num = min;
        }
        this.Capacity = num;
    }
}

MSDN:

Capacity is the number of elements that the ArrayList can store. Count is the number of elements that are actually in the ArrayList. Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • how is the capacity decided ? When it would increase ? Why it is 4 here, the elements are 3 so ?? 3+1 ? If i would have added 2, then capacity would be 3 ? – user2387900 Aug 04 '13 at 20:18
  • @user2387900: Edited my answer to include the .NET code. 4 is the minimum, then a doubling algorithm will double the size when required. Btw, i also suggest to use the strongly typed `List` instead which has the same doubling size algorithm. – Tim Schmelter Aug 04 '13 at 20:22
0

When you Add one element to the ArrayList it will creat the max memory space for 4 elements, and next if u add morethan 4 elements then it will automatically increase it to 8 and goes on.

ex: ArrayList arr = new ArrayList();

  arr.Add("xyz");
  arr.Add(23);
  arr.Add("abc"); 

For this it will shows the capacity as 4 and count as 3 and For this array if you add 2 more elements then capacity will be doubled that is 8 and count is 5.