It is a flexible collection class in which no size restriction or type restrictions.
Here is an example of Normal Array
int[] ia = new int[10];
in this array we can add only integers upto 10 elements.
ArrayList has no size restrictions. Here comes an example.
using System.Collections;
ArrayList al = new ArrayList();
private void buttonAdd_Click(object sender, EventArgs e)
{
al.Add("shan");
al.Add(12");
al.Add(DateTime.Now.ToString());
al.Add(true");
To display ArrayList elements in a listbox we can've three ways
1)for (int i = 0; i <>
listBox1.Items.Add(al[i].ToString());
2) foreach (object ob in al)
listBox1.Items.Add(ob.ToString());
3)
listBox1.DataSource = al;
}
private void buttonRemove_Click(object sender, EventArgs e)
{
listBox1.Items.Remove(listBox1.SelectedItem);
al.Remove("shan");//or
al.RemoveAt(2);
}
The drawback of ArrayList is if we try to sort the arraylist having different types , we will get error.
so we need a List which has all the facilities of arraylist , but permits only specific types. There comes the importance of List
using System.Collections.Generic;
List list1 = new List();
private void buttonaddList_Click(object sender, EventArgs e)
{
list1.Add(55);
list1.Add(44);
}
it always allows only integer insertions.
Stack
(Last In First Out)
we 've both generic as well as normal stacks. The main operations here are Push() and Pop()
Stack st = new Stack();
private void buttonAdd_Click(object sender, EventArgs e)
{
st.Push("Hello");
st.Push("hai");
st.Push("diya");
foreach (string s in st)
listBox1.Items.Add(s);
}
private void btnPop_Click(object sender, EventArgs e)
{
if (st.Count > 0)
{
MessageBox.Show(st.Pop());
}
else
MessageBox.Show("Stack is Empty");
}
private void btnPeek_Click(object sender, EventArgs e)
{
if (st.Count > 0)
{
MessageBox.Show(st.Peek());
}
else
MessageBox.Show("Stack is Empty");
}
Queue
Queue qq = new Queue();
Queue q = new Queue();
private void buttonAddqueue_Click(object sender, EventArgs e)
{
qq.Enqueue(1);
q.Enqueue("melbin");
q.Enqueue("Hello");
q.Enqueue("gigi");
foreach (string s in q)
listBox1.Items.Add(s);
}
private void buttondequeue_Click(object sender, EventArgs e)
{
if (q.Count > 0)
MessageBox.Show(q.Dequeue());
else
MessageBox.Show("Queue is Empty");
}
private void buttonpeek_Click(object sender, EventArgs e)
{
if (q.Count > 0)
MessageBox.Show(q.Peek());
else
MessageBox.Show("Queue is Empty");
}
It is easy to create our own generic classes
Here is a generic class for stack
class StackEg
{
T[] data = new T[100];
int pos=0;
public void push(T cnt)
{
data[pos++]=cnt;
}
public T pop()
{
return data[--pos];
}
Then we can easily create any type of stacks like the following
StackEg s1 = new StackEg();
StackEg s2 = new StackEg();
StackEg d1 = new StackEg();
Hash Table
Hashtable ht = new Hashtable(); //hash table works like a stack
private void btnHashAdd_Click(object sender, EventArgs e)
{
ht.Add(6,"av");
ht.Add(2, "Siju");
ht[3] = "fifi";
ht.Add(5, "piji");
ht.Add(4, "gojo");
//foreach (string s in ht.Values)
// listBox1.Items.Add(s);
foreach (DictionaryEntry dt in ht) //to retrieve both key and values
{
listBox1.Items.Add(dt.Key + " " + dt.Value);
}
}
private void btnHashRemove_Click(object sender, EventArgs e)
{
ht.Remove(1);
listBox1.Items.Clear();
foreach (DictionaryEntry dt in ht)
{
listBox1.Items.Add(dt.Key + " " + dt.Value);
}
}
SortedList
it automatically sortes its elements
SortedList st = new SortedList();
private void btnsortAdd_Click(object sender, EventArgs e)
{
st.Add(88, 123);
st.Add(99, "trtrt");
st.Add(24, "fghfs");
st.Add(22, "lll");
listBox1.Items.Clear();
foreach (DictionaryEntry dt in st)
{
listBox1.Items.Add(dt.Key + " " + dt.Value);
}
}
HybridDictionary
using System.Collections.Specialized ;
HybridDictionary hb = new HybridDictionary(); //its behaviour is like a queue
private void btnHybridAdd_Click(object sender, EventArgs e)
{
hb.Add(2, 123);
hb[3] = "dfgdfgdf";
hb.Add(1, "Ps");
listBox1.Items.Clear();
foreach (DictionaryEntry dt in hb)
{
listBox1.Items.Add(dt.Key + " " + dt.Value);
}
}
0 comments:
Post a Comment