2021-7-28 集合的基本练习

using System;
using System.Collections;

namespace 预处理指令
{
    class Program
    {

        static void Main(string[] args)
        {
            GetArrayList();
            GetStack();
            Console.ReadKey();
        } 
        public static void GetArrayList()
        {
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < 10; i++)
            {
                Random random = new Random();
                var a = random.Next(1, 100);
                arrayList.Add(a);
            }
            arrayList.Sort();
            foreach (var item in arrayList)
            {
                Console.Write(item+"\t");
            }
        }
        public static void GetStack()
        {
            Stack stack = new Stack();
            stack.Push('H');
            stack.Push('A');
            stack.Push('L');
            stack.Push('L');
            stack.Push('0');
            Console.WriteLine(stack.Peek());//返回最顶部的值
            stack.Pop();//移除最后进去的值
            Console.WriteLine(stack.Peek());
        }
    }
}
View Code

 完整版

using System;
using System.Collections;

namespace 预处理指令
{
    class Program
    {

        static void Main(string[] args)
        {
            GetArrayList();
            GetStack();
            GetQueue();
            Console.ReadKey();
        } 
        public static void GetArrayList()
        {
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < 10; i++)
            {
                Random random = new Random();
                var a = random.Next(1, 100);
                arrayList.Add(a);
            }
            arrayList.Sort();
            foreach (var item in arrayList)
            {
                Console.Write(item+"\t");
            }
        }
        public static void GetStack()
        {
            Stack stack = new Stack();
            stack.Push('H');
            stack.Push('A');
            stack.Push('L');
            stack.Push('L');
            stack.Push('0');
            Console.WriteLine(stack.Peek());//返回最晚进去的值
            stack.Pop();//移除最后进去的值(即先进后出)
            Console.WriteLine(stack.Peek());
        }
        public static void GetQueue()
        {
            Queue queue = new Queue();
            queue.Enqueue('H');
            queue.Enqueue('A');
            queue.Enqueue('L');
            queue.Enqueue('L');
            queue.Enqueue('0');
            Console.WriteLine(queue.Peek());//返回最早进去的值
            queue.Dequeue();//和栈不同的是删除最早进入的值(先进先出)
            Console.WriteLine(queue.Peek());
        }
    }
}
View Code

 

posted @ 2021-07-28 16:36  月长生  阅读(24)  评论(0)    收藏  举报