1.Stack类
栈,后进先出。push方法入栈,pop方法出栈。
Stack sk = new Stack();
Stack sk2 = new Stack();
int[] ll = new int[] { 1, 2, 3, 4, 5, 6 };
foreach (var item in ll)
{
sk.Push(item);
sk2.Push(item);
}
//进栈前
Console.WriteLine("进栈前:");
foreach (var item in ll)
{
Console.Write(item + ",");
}
Console.WriteLine();
Console.WriteLine("sk:");
foreach (var i in sk)
{
Console.Write(i + ",");
}
Console.WriteLine();
Console.WriteLine("sk2:");
foreach (var i in sk2)
{
Console.Write(i+",");
}
Console.WriteLine();
sk.Peek();//peek方式返回顶部对象不移除
Console.WriteLine("sk(Peek):");
foreach (var i in sk)
{
Console.Write(i + ",");
}
Console.WriteLine();
sk2.Pop();//pop方式返回顶部对象不移除
Console.WriteLine("sk2(pop):");
foreach (var i in sk2)
{
Console.Write(i + ",");
}
Console.WriteLine();
Console.ReadLine();
2.Queue类
队列,先进先出。enqueue方法入队列,dequeue方法出队列
Queue q = new Queue();
Queue q1 = new Queue();
int[] ll = new int[] { 1, 2, 3, 4, 5, 6 };
foreach (var item in ll)
{
q.Enqueue(item);
q1.Enqueue(item);
}
//进队列前
Console.WriteLine("进栈前:");
foreach (var item in ll)
{
Console.Write(item + ",");
}
//出队列Dequeue
Console.WriteLine();
Console.WriteLine("出队列(Dequeue):");
q.Dequeue();
foreach (var item in q1)
{
Console.Write(item + ",");
}
Console.ReadLine();
3.HashTable类
哈希表,键对值。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。
Hashtable Ht = new Hashtable();
Ht.Add("老大", "呆");
Ht.Add("老二", "傻");
Ht.Add("老三", "2");
Console.WriteLine("输出Hash键:");
foreach (var item in Ht.Keys)
{
Console.WriteLine(item);
}
Console.WriteLine("输出Hash键 Hash值:");
foreach (DictionaryEntry item in Ht)
{
Console.WriteLine("{0}:{1}", item.Key, item.Value);
}
Console.ReadLine();
上课去了待会再写…………