1026集合类

51 阅读1分钟

using System; using System.Collections; using System.Dynamic;

namespace demo1026._1 { internal class Program { public static void getBin(int num) { Stack stack = new Stack(); while (num > 0) { int remainer = num % 2; stack.Push(remainer); num /= 2;

        }
        foreach(int i in stack)
        {
            Console.Write(i);
        }
        Console.WriteLine();
    }
    static void Main(string[] args)
    {
        //我们前面已经学了其他的集合类  列表,动态数组,字典
        //栈,队列,哈希表,链表
        //栈 先进后出
        // Stack stack = new Stack();
        //入栈
        /* stack.Push("asdadf");
         stack.Push(12);
         stack.Push(12.1);
         //只能读取栈顶的数据
         Console.WriteLine(stack.Peek());//12.1
         //出栈
         stack.Pop(); 
         Console.WriteLine(stack.Peek());//12
         //查有没有这个数据
         Console.WriteLine(stack.Contains(12.1));//false
         //遍历栈
         while (stack.Count>0)//判断栈的长度时候大于0  如果大于0那就读取栈顶的信息  每读取一个 取出一个
         {
             Console.WriteLine(stack.Peek());
             stack.Pop();

         }
         //清空栈
         stack.Clear();
         //Console.WriteLine(stack.Peek());//报错
         stack.Push("sfcfa");
         stack.Push(23);

         foreach(object i in stack)//使用foreach没有进行出栈的操作,所以最后栈还是原来的样子
         {
             Console.WriteLine(i);
         }
         Console.WriteLine(stack.Peek());//查看有没有清空*/

        //求任意二进制数
        /*int a = Convert.ToInt32(Console.ReadLine());
        int b = 0;
        while (a > 0)
        {
            b = a % 2;
            stack.Push(b);
            a = a / 2;
        }
        while (stack.Count > 0)
        {
            Console.Write(stack.Peek());
            stack.Pop();

        }*/

        /*while (true)
        {
            int num = Convert.ToInt32(Console.ReadLine());
            getBin(num);
        }*/


        /* //创建一个队列 
         Queue<string> queue = new Queue<string>();
         //入队列
         queue.Enqueue("asdad");
         queue.Enqueue("nbdg");
         //查看队头的值
         Console.WriteLine(queue.Peek());
         //查看某个数据是否在队中
         Console.WriteLine(queue.Contains("asdad"));
         //出队列
         queue.Dequeue();
         Console.WriteLine(queue.Peek());
         queue.Enqueue("knjnj");
         queue.Enqueue("knasdasdasdjnj");

         //遍历
         foreach(string i in queue)
         {
             Console.WriteLine(i);
         }
         Console.WriteLine(queue.Peek());//查看有没有清空

         while (queue.Count > 0)
         {

             Console.WriteLine(queue.Peek());
             queue.Dequeue();
         }

         //清空队列
         queue.Clear();*/

        /*Queue<string> queue = new Queue<string>();
        string str = Console.ReadLine();
        string[] str1 = str.Split(" ");
        foreach(string i in str1)
        {
            queue.Enqueue(i);
        }
        while (queue.Count > 0)
        {

            Console.WriteLine(queue.Peek());
            queue.Dequeue();
            Thread.Sleep(1000);
        }*/

        /*List<string> list = new List<string>() { "a", "b", "c", "d", "e", "f", "g", "h", "i", "k" };
        Random random = new Random();
        Queue queue = new Queue();

        for(int i = 0; i < 10; i++)
        {
            queue.Enqueue(list[random.Next(0, list.Count)]);
        }
        foreach(string news in queue)
        {
            Console.WriteLine(news);
            Thread.Sleep(1000);
        }*/

        Hashtable hashtable = new Hashtable();
        hashtable.Add("name", "小明");
        hashtable.Add("age", 13);
        hashtable.Add("high", 190);

        try
        {
            hashtable.Add("name", "小明");//键名在哈希表中是唯一的

        }
        catch
        {
            Console.WriteLine("重复定义了简明");
        }

        Console.WriteLine(hashtable["name"]);//通过键名找到值  如果没有该键名读取到的就是空
        //移除键值对
        hashtable.Remove("high");
        //查看是否含有该键值对
        Console.WriteLine(hashtable.ContainsKey("high"));//false
        Console.WriteLine(hashtable.Contains("high"));//false
        Console.WriteLine(hashtable.ContainsValue(13));//True

        //如何遍历哈希表
        foreach (object i in hashtable.Keys)
        {
            Console.WriteLine(i);
        }
         foreach (object i in hashtable.Values)
        {
            Console.WriteLine(i);
        } 
        foreach (DictionaryEntry i in hashtable)
        {
            Console.WriteLine(i.Key);
            Console.WriteLine(i.Value);
        }



    }
}

}