特殊集合 队列(queue) 堆栈(stack) 哈希表(hashtable)

1.stack栈集合;又名 干草堆集合 栈集合

特点:(1)一个一个赋值 一个一个取值
(2)先进后出
实例化 初始化
Stack st = new Stack();
//添加元素用push
st.Push(2);
st.Push(6);
st.Push(9);
st.Push(5);
st.Push(1);
输出个数
Console.WriteLine(st.Count);

只要使用一次pop方法,就会从最后一个元素开始排除 弹出
Console.WriteLine(st.Pop());


只想查看不弹出
Console.WriteLine(st.Peek());

遍历集合
foreach (int aa in st)
{
Console.WriteLine(aa);
}


2,queue队列集合;特点:先进先出

实例化 初始化
Queue que = new Queue();
添加元素

que.Enqueue(5);
que.Enqueue(2);
que.Enqueue(9);
que.Enqueue(8);
que.Enqueue(1);

 


移除一个元素 从头开始
que.Dequeue();

个数
Console.WriteLine(que.Count);


遍历集合
foreach(int aa in que)
{
 Console.WriteLine(aa);
}

3,HashTable 哈希表集合

特点:先进后出 一个一个赋值,但是只能一起取值
实例化 初始化
Hashtable ht = new Hashtable();
添加元素

复制代码
ht.Add(1, "张三");
ht.Add(2, "李四");
ht.Add(3, "王五");
ht.Add(4, "赵六");
ht.Add(5, "冯七");
ht.Add(6, "钱八");
复制代码

 



读取

复制代码
foreach (object aa in ht.Keys)//单纯的存储key的集合
{
 Console.WriteLine(aa);
}
foreach (string bb in ht.Values)单纯的存贮value的集合
{
 Console.WriteLine(bb);
}
复制代码

 


使用枚举类型进行读取,排列成表格

IDictionaryEnumerator ide = ht.GetEnumerator();
while(ide.MoveNext())
{
 Console.WriteLine(ide.Key+"\t"+ide.Value);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值