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.

Reversing ArrayList Items

The ArrayList class provides a method named Reverse. In its simplest form the method reverses the order of the items in the collection, as shown below:

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

// Reverse items
myCollection.Reverse();

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

/* OUTPUT

Five
Four
Three
Two
One

*/

If only a section of the ArrayList contents are to be reversed, two integer parameters are used with the Reverse method. The first specifies the index of the first item in the collection that is to be affected. The second parameter is used to determine how many items in the collection will be reversed. In the following example, the first and last entries in the ArrayList remain in place whilst the middle of the collection is reversed.

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

// Reverse items
myCollection.Reverse(1,3);

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

/* OUTPUT

One
Four
Three
Two
Five

*/

Querying ArrayList Contents

As the ArrayList implements IList, it supports the two standard query methods for lists. These are Contains and IndexOf. The ArrayList method provides further methods that can be used to search the contents of the collection. These are described below.

Retrieving an Entry's Index

The IList interface's IndexOf method searches for a specified object within a collection and, if found, returns the index of the first matching item. This functionality is extended for ArrayLists with the addition of two overloaded versions of the method.

The first overloaded version of IndexOf accepts an additional integer argument, which specifies the index position from which a search should commence. Any items with a lower index are excluded from the search. This provides an advantage over the basic version of the method in that multiple occurrences of the same value may be easily located.

The following example loops until every occurrence of the text, "Find me", is found. The loop ends when the index value becomes -1, indicating that the search object was not found.

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

int location = -1;

do
{
    location = myCollection.IndexOf("Find Me", location + 1);
    if (location != -1)
    {
        Console.WriteLine("Found at index {0}", location);
    }
}
while (location != -1);

/* OUTPUT

Found at index 2
Found at index 4
Found at index 6

*/

The second overloaded IndexOf method adds a second integer parameter. The second parameter determines the size of the range to search within the ArrayList. Items that appear in the collection earlier than the first index or after the end of the search range are not matched.

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

Console.WriteLine(myCollection.IndexOf("One",1,3));     // Outputs -1
Console.WriteLine(myCollection.IndexOf("Three",1,3));   // Outputs 2
Console.WriteLine(myCollection.IndexOf("Five",1,3));    // Outputs -1

If you wish to search backwards from the end of an ArrayList, use the ArrayList's LastIndexOf method. The parameters that can be specified match those of the IndexOf method. When a second argument is used to specify the range size, this range starts at the specified last index and counts backwards.

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

Console.WriteLine(myCollection.LastIndexOf("One"));     // Outputs 0
Console.WriteLine(myCollection.LastIndexOf("One",3,3)); // Outputs -1
5 May 2007