Creating an ArrayList of Objects c#

  • Thread starter Thread starter Netmonster
  • Start date Start date
N

Netmonster

Hello,

Does any one have an example of how to create an ArrayList of objects?
i.e. ArrayList of ArrayLists or an ArrayList of Hashtables?
Thanks in advance

KC
 
KC,

It's like adding any other item to an ArrayList, and then retrieving
that.

For example, if you wanted to add a Hashtable to an ArrayList, then you
would do this:

// An array list.
ArrayList arrayList = new ArrayList();

// Add 100 hashtables.
for (int index = 0; index < 100; index++)
{
// Add a hashtable.
arrayList.Add(new Hashtable());
}

Now if you want to use one of the hashtables, you would have to cast the
item that the array list returns:

// Get the first hashtable.
Hashtable hashTable = (Hashtable) arrayList[0];

Hope this helps.
 
arraylist is dynamic

ie..

ArrayList x1 = new ArrayList();

x1.Add (your object here)
x1.Add( another object here)
etc....
 
Netmonster,

The array list is self-allocating. So you just keep calling Add when
you need to in order to add a new Hashtable. You don't have to know how
many you need beforehand.
 
Back
Top