Code Heaven For C# Developer

Monday, May 17, 2010

Hash Table And ArrayList

CREATE AN ARRAYLIST

The ArrayList object is a collection of items containing a single data value.

Items are added to the ArrayList with the Add() method.

The following code creates a new ArrayList object named mycountries and four items are added:


ArrayList mycountries=New ArrayList();
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")

By default, an ArrayList object contains 16 entries. An ArrayList can be sized to its final size with the TrimToSize() method:


ArrayList mycountries=New ArrayList();
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()

An ArrayList can also be sorted alphabetically or numerically with the Sort() method:
To sort in reverse order, apply the Reverse() method after the Sort() method:

ArrayList mycountries=New ArrayList();
mycountries.Add("Norway")
mycountries.Add("Sweden")
mycountries.Add("France")
mycountries.Add("Italy")
mycountries.TrimToSize()
mycountries.Sort()
mycountries.Reverse()


CREATE A HASHTABLE

HashTable is a collection that is used to store a value and key pair objects

Hashtable _simpleTable = new Hashtable();


Add some data in it.

_simpleTable.Add("Key1", "Value1");
_simpleTable.Add("Key2", "Value2");


Get the size.


_simpleTable.Count;

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home