BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

The ArrayList Collection

The thirty-seventh part of the C# Fundamentals tutorial describes the use of the ArrayList class. This general-purpose collection class provides the ability to create and manipulate a group of related objects as if they were a variable-length array.

The ArrayList Collection

When using one-dimensional arrays, the array is defined with a fixed length. This can be a drawback if the number of elements to be held in an array is variable and unknown until run-time.

The ArrayList is a general-purpose collection provided by the .NET framework. It stores a set of objects that is flexible in size and includes functionality to control the collection contents, including the ability to modify ranges of values, perform sort operations and search the contents of the ArrayList.

Implemented Collection Interfaces

In the previous article of the C# Fundamentals tutorial I reviewed interfaces used by collections. The ArrayList implements ICollection and IList; all of the properties and methods defined by these interfaces are supported by ArrayList. These behaviours are not addressed in this article. Instead, we will look at the additional behaviour that is specific to ArrayLists.

Declaring an ArrayList

Constructors

The ArrayList class provides three standard constructors that allow a new collection to be instantiated and populated. The first is the simplest, requiring no parameters. The new collection is generated with the capacity to store several items. Each time the capacity is exceeded, it is doubled automatically.

The ArrayList class is found in the System.Collections namespace so to execute the examples, add using System.Collections; to the source code.

ArrayList myCollection = new ArrayList();

If the maximum number of items that will be stored in an ArrayList is known before the collection is created, it is more efficient to specify it. If the size is underestimated and is later exceeded, the capacity will still be doubled to accommodate extra items. To declare the initial capacity of an ArrayList provide the value as an integer in the constructor.

ArrayList myCollection = new ArrayList(5);

The third constructor for the ArrayList allows the entire contents of the new collection to be initialised using the contents of any other array or collection that supports the ICollection interface. The array or collection is passed as an argument to the constructor. Each item is added to the new ArrayList in the order that they are enumerated from the source collection. The following example uses an array of strings to initialise an ArrayList.

string[] fruitArray = new string[] {"Apple", "Banana", "Grape"};

ArrayList fruitCollection = new ArrayList(fruitArray);

// List collection items
foreach (string fruit in fruitCollection)
{
    Console.WriteLine(fruit);
}

/* OUTPUT

Apple
Banana
Grape

*/

ArrayList.Repeat

It can be useful to create a new ArrayList collection that is pre-populated with multiple instances of the same value or object. The static Repeat method provides this functionality:

ArrayList myCollection = ArrayList.Repeat("Item",5);

// List collection items
foreach (string s in myCollection)
{
    Console.WriteLine(s);
}
    
/* OUTPUT

Item
Item
Item
Item
Item

*/

NB: If an ArrayList is populated with a reference type using Repeat, all of the items in the collection will be references to the same object. ie. changing the properties of one item in the collection will give the impression that all of the other items have also changed because they reference the same data. This does not occur when populating with value types.

5 May 2007