Using .NET Collections, Hashtables and ArrayLists
While everyone is familiar with arrays you may not be familiar with an
arraylist. Arraylists are similar to arrays but are represented by
the System.Collections.ArrayList class and have
the ability to grow.
Arraylists let you store any type of object and you can retrieve stored objects
using an index just like a regular array or you can enumerate through the
objects. The code below shows how you can use an arraylist.
Arraylists let you store any type of object and you can retrieve stored objects
using an index just like a regular array or you can enumerate through the
objects. The code below shows how you can use an arraylist.
Dictionaries or Hash Tables
also let you store any type of object but you store them in
key/value pairs. This way you can retrieve an item by a value
instead of a numeric index. You can add and remove items to/from a Dictionary
without the performance penalty of an ArrayList. .NET also contains a
specialized for of hash table called a NameValueCollection
which is described on my NameValueCollection
page.
Sample ArrayList code:
//
// Instantiate an arraylist with a capacity of 20 items. This is not
// Necessary but improves efficiency if you know the max size up front.
//
ArrayList alData = new ArrayList(20);
//
// Add items to the arraylist.
//
for (int i = 0; i < 10; i++)
{
alData.Add("Item" + i.ToString());
}
//
// See if we have items in the arraylist. Get the first item.
//
if (alData.Count > 0)
{
Console.WriteLine("Item 1 is: " + alData[0]);
}
//
// Enumerate through the arraylist.
//
if (alData.Count != 0)
{
IEnumerator Enumerator = alData.GetEnumerator();
while (Enumerator.MoveNext())
{
Console.WriteLine(Enumerator.Current.ToString());
}
}
//
// See if the arraylist contains a specific item.
//
if (alData.Contains("Item2"))
{
Console.WriteLine("Found it");
}
//
// Remove an arraylist item based on its value.
//
alData.Remove("Item2");
Similar code using a Hashtable or Dictionary follows. Note that when
iterating through a hashtable the items are retrieved in no specific order.
They may not be retrieved in the same order that you added them, unlike an
ArrayList.
//
// Instantiate a hashtable.
//
Hashtable hshData = new Hashtable();
//
// Add key/value pairs to the hashtable.
//
for (int i = 0; i < 10; i++)
{
hshData.Add(i, "Item" + i.ToString());
}
//
// See if we have items in the hashtable.
// Get the first item by specifying its key.
//
if (hshData.Count > 0)
{
Console.WriteLine("Item 1 is: " + hshData[0]);
}
//
// Enumerate through the hashtable.
//
if (hshData.Count != 0)
{
IDictionaryEnumerator Enumerator = hshData.GetEnumerator();
while (Enumerator.MoveNext())
{
Console.WriteLine("Key: " + Enumerator.Key.ToString());
Console.WriteLine("Value: " + Enumerator.Value.ToString());
}
}
//
// Enumerate through the hashtable's keys.
//
if (hshData.Count != 0)
{
ICollection myKeys = hshData.Keys;
foreach (object Key in MyKeys)
{
Console.WriteLine(Key.ToString());
}
}
//
// See if the hashtable contains a specific key or value.
//
if (hshData.ContainsValue("Item2"))
{
Console.WriteLine("Found value");
}
if (hshData.ContainsKey(2))
{
Console.WriteLine("Found key");
}
//
// Remove a hashtable item based on its key.
//
hshData.Remove(2);
|