Hash table
Array List
Content: 21 23 45 22 56 47 37
Sorted Content: 21 22 23 37 45 47 56
- Hash table is collection of key value pairs.
- Each element is key value pair and stored in a DictionaryEntry object.
- A value can be null but a key cannot be null.
- In hash table we access elements by keys.
Hastable Example
using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
Hashtable htable = new Hashtable();
htable .Add("01", "Tejpal");
htable .Add("02", "Bhavnish");
htable .Add("03", "Prince");
htable .Add("04", "Sandu");
htable .Add("05", "Kiran");
if (htable .ContainsValue("Tejpal"))
{
Response.Write("Tejpal is already in the list.<br/><br/>");
}
else
{
htable .Add("06", "Tejpal");
}
// Get a collection of the keys.
ICollection key = htable .Keys;
foreach (string k in key)
{
Response.Write(k + ": " + htable [k]);
}
}
OutPut
Tejpal is already in the list
04: Sandu 03: Prince 02: Sandu 01: Tejpal 05: kiran
- Array list is a collection of objects in array whose size can be dynamically increased according to the requirement.
- In array list we can add data of any data type, Every item in array list is treated as object.
- In array list we can access elements by using index.
ArrayList Example
using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
ArrayList arrLst = new ArrayList();
Console.WriteLine("Adding numbers:");
arrLst l.Add(21);
arrLst .Add(23);
arrLst .Add(45);
arrLst .Add(22);
arrLst .Add(56);
arrLst .Add(47);
arrLst .Add(37);
Response.Write("Capacity: {0}" + arrLst .Capacity + "<br/>");
Response.Write("Count: {0}" + arrLst .Count + "<br/>");
Response.Write("Content: ");
foreach (int i in arrLst )
{
Response.Write(i + " ");
}
Response.Write("<br/>Sorted Content is: ");
arrLst.Sort();
foreach (int i in arrLst )
{
Response.Write(i + " ");
}
}
Output
Capacity: {0}8
Count: {0}7Content: 21 23 45 22 56 47 37
Sorted Content: 21 22 23 37 45 47 56
No comments:
Post a Comment