zoukankan      html  css  js  c++  java
  • C#部分---特殊集合:stack栈集合、queue队列集合、哈希表集合。

    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+"	"+ide.Value);
    }
  • 相关阅读:
    Oracle to_char格式化函数
    电脑快捷键大全
    Failed to create the Java Virtual Machine (Myeclipse或者eclipse启动报错)
    Java 面试题
    UVA1108 Mining Your Own Business
    无向图的连通性
    [NOI Online #2 提高组]子序列问题
    [NOI Online #3 提高组]优秀子序列
    POJ2430 Lazy Cows
    UVA1633 Dyslexic Gollum
  • 原文地址:https://www.cnblogs.com/xingyue1988/p/5967558.html
Copyright © 2011-2022 走看看