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.

Modifying ArrayList Contents

The ArrayList is designed to provide a flexible array-like collection of objects. It includes many methods that permit modification of its contents. The following sections describe the modification methods that have not been covered in the collection interfaces article.

Adding Multiple Items

The ArrayList contains two methods that allow several items to be added in a single command. In each case, the additional items are held in a collection or array that implements ICollection.

The AddRange method adds the entire contents of a collection to the end of an ArrayList. The ICollection object holding the items to be appended is passed as the only parameter.

ArrayList myCollection = ArrayList.Repeat("Item",3);
ArrayList extraItems = ArrayList.Repeat("Extra",2);

// Add extra items
myCollection.AddRange(extraItems);

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

/* OUTPUT

Item
Item
Item
Extra
Extra

*/

The InsertRange method provides a similar function to AddRange. However, the method requires an additional parameter to indicate the index at which the new items should be inserted. Existing items in the ArrayList are moved to a higher index to allow the insertion.

ArrayList myCollection = ArrayList.Repeat("Item",3);
ArrayList extraItems = ArrayList.Repeat("Extra",2);

// Add extra items
myCollection.InsertRange(1,extraItems);

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

/* OUTPUT

Item
Extra
Extra
Item
Item

*/

Removing Multiple Items

ArrayLists allow a range of items to be deleted from a collection in a single command using the RemoveRange method. RemoveRange accepts two parameters. The first specifies the index of the first item to be deleted. The second parameter indicates the number of concurrent collection entries to be removed.

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

// Remove three items starting with the first item in the ArrayList
myCollection.RemoveRange(0,3);

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

/* OUTPUT

Item
Item

*/

Replacing Multiple Items

Often it is useful to replace the contents of part of one ArrayList with the items from another. You can do this using the SetRange method. SetRange requires two parameters: the index of the first item to overwrite and the collection to be used to replace existing entries from that position forwards. The replacement items must be in an ICollection-based array or collection.

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

// Overwrite items
myCollection.SetRange(1,newItems);

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

Item
New
New
Item
Item

*/
5 May 2007