首页 > 其他 > 详细

特殊集合 Stack Queue Hashtable

时间:2016-10-16 18:37:46      阅读:251      评论:0      收藏:0      [点我收藏+]

//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);
            }
技术分享

  

 //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);
            }
技术分享

  

//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

原文:http://www.cnblogs.com/maxin991025-/p/5966630.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!