4

Does an ArrayList internally use an Array? Is this an empty array if we use the default constructor (new ArrayList())? Thanks.

4
  • 3
    Why do you want to know - one point of OO programming is encapsulation so that you do not need to known the internals of a class just use its interface? (As usual nothing is perfect and their might ne reasons to have the knowledge) Commented Apr 24, 2010 at 22:01
  • 3
    @Mark: The purpose of encapsulation is not that you're completely oblivious of what's going on inside; it's that you don't have to know what's going on inside. It's always useful to understand the internals of a system. By your logic, there should be no need for both LinkedList<T> and List<T>, since they both do the same thing, right? Commented Apr 24, 2010 at 22:08
  • 2
    @Mark - It's good to know so that you learn how smart people have done what they do. @rkrauter - I would recommend that you go look at the .NET Framework Library Source Code (Yes it is available) if you really want to know. weblogs.asp.net/scottgu/archive/2008/01/16/… Commented Apr 24, 2010 at 22:08
  • Not that this answers your question, but if you're using .NET 2.0 or higher then there's little reason to use the ArrayList anymore as we have a better (Generic) equivalent -- the List<T> Class... msdn.microsoft.com/en-us/library/6sh2ey19.aspx Commented Apr 24, 2010 at 22:15

3 Answers 3

7

Yes it does. One of the easiest ways to verify this is to look at the source. You can either get your hands on the reference source, or you can simply use .NET Reflector to decompile the .NET DLLs.

Here's the relevant part of ArrayList, from Reflector:

public class ArrayList : IList, ICollection, IEnumerable, ICloneable
{
    static ArrayList()
    {
        emptyArray = new object[0];
    }

    public ArrayList()
    {
        this._items = emptyArray;
    }

    private object[] _items;

    private static readonly object[] emptyArray;

    // etc...
}

You shouldn't rely on this always being the case. It is an implementation detail and could change in future versions of .NET (although it probably won't). Also for new code you should consider using List<T> instead of ArrayList.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Yes, the List<T> should definitely be used in place of this older collection.
So do all lists use some kind of fixed array internally. I noticed the stringbuilder also uses an array. So there is no dynamic memory? You have to preallocate some memory and fill it with stuff, when you need to add more stuff, you preallocate a larger memory area and start filling again with stuff? Only trying to learn internals. Thanks.
3

Yes, an ArrayList uses an array to store the items.

If you create an ArrayList without specifying a capacity, the default starting capacity is used. What the default starting capacity is may depend on the version of the framework. For framework 2 it seems to be zero. In framework 1 i think that it was 16.

1 Comment

Looking at the IL, ArrayList at first uses an empty array. When a value is added, the array is changed to one with 4 items. After that the capacity is doubled whenever the array is full.
2

Yes.. ArrayList uses an array in itself.

It holds a Object array (in java. c# should be the same too) to give you a homogenity. Although arraylist seem to be a very dynamic memory allocating class, its internal operations are full of arrays.

The time when you create an object by calling constructor, internally it invokes an array of say a limited size, may be 10 (infact exactly 10 in java). Then as and when you add objects to the arraylist it will have to increase the internal array as well. So the internal array's size has to be increased. So, a new array is created with double the size and the old values are copied onto this new array. Note the capacity of the array is increased so more objects can be added.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.