特殊集合 Stack Queue Hashtable

时间:2023-03-08 22:37:56
特殊集合 Stack   Queue   Hashtable

//Stack    干草堆集合    栈集合      先进后出

Stack st = new Stack();  //实例化   初始化
st.Push(2); //添加元素
st.Push(6);
st.Push(9);
st.Push(5);
st.Push(1);
Console.WriteLine(st.Count);
Console.WriteLine(st.Peek()); //peek方法,只查看不弹出
Console.WriteLine(st.Pop()); //只要使用pop方法,就会从最后一个元素弹出
Console.WriteLine(st.Count);
foreach (int aa in st) //遍历集合
{
Console.WriteLine(aa);
}

特殊集合 Stack   Queue   Hashtable

  

//Queue  队列集合     先进先出

Queue qu = new Queue();
qu.Enqueue(5); //添加元素
qu.Enqueue(6);
qu.Enqueue(9);
qu.Enqueue(8);
qu.Enqueue(1);
qu.Dequeue(); //删除一个元素,从头开始
Console.WriteLine(qu.Count); //个数
Console.WriteLine("-------");
foreach (int aa in qu) //遍历集合
{
Console.WriteLine(aa);
}

特殊集合 Stack   Queue   Hashtable

  

//Hashtable  哈希表集合   先进先出,一个一个赋值,但只能一起取值

Hashtable ht = new Hashtable();
ht.Add(1, "张三"); //添加元素,一个位置包含两个值,一个是key,一个是value。
ht.Add(2, "李四");
ht.Add(3, "王五");
ht.Add(4, "赵六");
ht.Add(5, "冯七"); foreach (int aa in ht.Keys)
{
Console.WriteLine(aa);
}
foreach (string bb in ht.Values)
{
Console.WriteLine(bb);
} IDictionaryEnumerator ide = ht.GetEnumerator();//使用枚举类型进行读取,排列成表格
while (ide.MoveNext())
{
Console.WriteLine(ide.Key + " " + ide.Value);
}

特殊集合 Stack   Queue   Hashtable