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.

Sorting an ArrayList

Some ArrayList operations require the collection to be sorted. The ArrayList's Sort method can be used to order items in an array. If used without parameters, the items within the array are sorted into ascending order. In the case of numeric and string values, this order is numeric or alphabetic.

When an ArrayList contains objects, each object must implement the IComparable interface and provide compatible comparisons. This interface provides the information needed to determine the ordering. The ordering of objects using IComparable is beyond the scope of a basic tutorial. It is important to note that the object types must be comparable. It is not possible, for example, to sort an ArrayList containing both alphanumeric strings and integer values.

The following example uses the Sort method to order an ArrayList containing strings:

ArrayList myCollection = new ArrayList(
    new string[] {"One", "Two", "Three", "Four", "Five"});

// Sort items
myCollection.Sort();

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

/* OUTPUT

Five
Four
One
Three
Two

*/

NB: There are further overloaded versions of Sort. These allow different sorting options to be specified and permit the sorting of a range of values in an ArrayList.

Performing a Binary Search

A binary search is a more efficient method by which an item in a sorted list can be found. Instead of looping through every value in the collection, the middle value is checked. If this value is larger than the value to find, another item half way between the low value and the initial lookup is checked. If the value is smaller, the opposite occurs. Each time an item is compared in the ArrayList, half of the remaining items can be immediately discounted as potential matches. To demonstrate the efficiency difference, a search of a 65,536 item ArrayList using IndexOf may require 65,536 individual comparisons. When using a binary search algorithm, the maximum number of tests is sixteen.

The BinarySearch method allows fast searching of a sorted ArrayList. If the ArrayList is not sorted the results may be incorrect. It should be noted that when duplicates exist in the collection, the item found by a binary search is not necessarily the one with the lowest index number.

ArrayList myCollection = new ArrayList(
    new string[] {"One", "Two", "Three", "Four", "Five"});

// Sort items
myCollection.Sort();

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

// Perform a search
Console.WriteLine("'One' found at index {0}", myCollection.BinarySearch("One"));

/* OUTPUT

Five
Four
One
Three
Two
'One' found at index 2

*/

Extracting a Range of Values

You can use the GetRange method to extract a range of values from an ArrayList. The method allows a start index and number of items to be specified. The corresponding items are returned in a new ArrayList.

ArrayList myCollection = new ArrayList(
    new string[] {"One", "Two", "Three", "Four", "Five"});

// Extract items
ArrayList extract = myCollection.GetRange(1,3);

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

/* OUTPUT

Two
Three
Four

*/
5 May 2007