Collection Objects

Collection Objects

The System.Collections namespace contains a number of useful variable length array objects you can use to add and obtain items in several ways.

 

ArrayLists

The ArrayList object is essentially a variable length array that you can add items to as needed. The basic ArrayList methods allow you to add elements to the array and fetch and change individual elements:

 

float[] z = {1.0f, 2.9f, 5.6f};

ArrayList arl = new ArrayList ();

for (int j = 0; j< z.Length ; j++) {

arl.Add (z[j]);

}

 

The ArrayList has a Count property you can use to find out how many elements it contains. You can then move from 0 to that count minus one to access these elements, treating the ArrayList just as if it were an array:

 

for (j = 0; j < arl.Count ; j++) {

Console.WriteLine (arl[j]);

}

 

You can also access the members of ArrayList object sequentially using the foreach looping construct without needing to create an index variable or know the length of the ArrayList:

foreach (float a in arl)

{

Console.WriteLine (a);

}

You can also use the methods of the ArrayList shown in Table 1.

Clear

Clears the contents of the ArrayList

Contains(object)

Returns true if the ArrayList contains that value

CopyTo(array)

Copies entire ArrayList into a one-dimensional array.

IndexOf(object)

Returns the first index of the value

Insert(index, object)

Insert the element at the specified index.

Remove(object)

Remove element from list.

RemoveAt(index)

Remove element from specified position

Sort

Sort ArrayList

Table 1- ArrayList methods

 

An object fetched from an ArrayList is always of type object. This means you usually need to cast the object to the correct type before using it:

 

float x = (float) arl[j];

Hashtables:

A Hashtable is a variable length array where every entry can be referred to by a key value. Typically, keys are strings of some sort, but they can be any sort of object. Each element must have a unique key, although the elements themselves need not be unique. Hashtables are used to allow rapid access to one of a large and unsorted set of entries, and can also be used by reversing the key and the entry values to create a list where each entry is guaranteed to be unique.

 

Hashtable hash = new Hashtable ();

float shashiray = 12.3f;

hash.Add ("shashi", shashiray); //add to table

//get this one back out

float temp = (float)hash["shashi"];

 

Note that like the ArrayList, we must cast the values we obtain from a Hashtable to the correct type. Hashtables also have a count property and you can obtain an enumeration of the keys or of the values.

 

SortedLists:

The SortedList class maintains two internal arrays, so you can obtain the elements either by zero-based index or by alphabetic key.

 

float satyamray = 44.55f;

SortedList slist = new SortedList ();

slist.Add ("shashi", shashiray);

slist.Add ("satyam", sammyray);

//get by index

float newShashi = (float)slist.GetByIndex (0);

//get by key

float newSatyam = (float)slist["Satyam"];

 

You will also find the Stack and Queue objects in this namespace. They behave much as you'd expect, and you can find their methods in the system help documentation.

 

Shashi Ray
Ebook Download
View all
Learn
View all