Collections
In C#, we have five types of collections.they are
· ArrayList
· Bit Array
· Hashtable
· Queue
· Stack
ArrayList:
In C# ,Arraylist is an array that can change its size according to our need.
It can support builtin types and custom types.
We have predefined methods in arraylist they are
· Add
· Insert
· Remove
· RemoveAt
· Clear
· Clone
· Equals
· Reverse
· To array
· ToString
Example:
Class program
{
Public static void main(string []args)
{
ArrayList t=new ArrayList(10);
Int i=0;
t.Add(i);
t.insert(1,++i);
foreach (int y in t)
{
Console.writeline(y);
}
t.remove(i);
t.remove(0);
t.clear();
}
}
HashTables
Hashtables are combination of keys and values that are placed according to the hashcode of key.
By hashcode key we place the objects in the hashtable.
The below example give clear idea about the hashtable.
Example:
Class hashtableexample
{
Static void main(string []args)
{
Hashtable x=new Hashtable(10);
Hashtable.synchronized(x);
x.Add(100,”Arrays”);
x.Add(200,”delegates”);
foreach (dictionaryentry i in x)
{
Console.writeline(i.value);
}
x.remove(100);
x.remove(200);
x.clear();
}
}
QUEUES
a queue represents collection of objects as first in first out(fifo) method.
In queue, the records must be entered at one end and removed from other end.
Example:
Class QueueExample
{
Static void main(string []args)
{
Queue q=new Queue(10);
int i=0;
q.Enqueue(i);
i++;
q.Enqueue(i);
foreach(int y in q)
{
Console.writeline(y);
}
q.Dequeue();
q.clear();
}
}
No comments:
Post a Comment