您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Unity3D开发-C#语言进阶篇(非泛型集合应用)

Unity3D开发-C#语言进阶篇(非泛型集合应用)

来源:伴沃教育
  class Program
    {
        static void Main(string[] args)
        {

            //---------------第一题-----------------

            ArrayList list = new ArrayList(new int[20]);

            // list.Capacity = 20;

            Stack st = new Stack();

            Random r = new Random();
            for (int i = 0; i < list.Capacity; i++)
            {


                list[i] = r.Next(0, 50);
                if ((int)list[i] % 2 == 1)
                {
                    st.Push(list[i]);
                }

                else if ((int)list[i] % 2 == 0)
                {
                    if (st.Count == 0)
                    {
                        continue;
                    }
                    else
                    {
                        st.Pop();
                    }

                }

            }


            foreach (int i in st)
            {
                Console.Write(i + " ");

            }

            Console.WriteLine();


            //--------第二题------------------
            // 2、arraylist = {2,41,5,12,42,41,87},把其41之间的数复制到另外一个arraylist集合列表中。
            ArrayList list1 = new ArrayList(new int[] { 2, 41, 5, 12, 42, 41, 87 });
            int index1 = list1.IndexOf(41);
            int index2 = list1.LastIndexOf(41);

            ArrayList list2 = new ArrayList(new int[list1.GetRange(index1 + 1, index2 - index1 - 1).Count]);

            // list2.AddRange(list1.GetRange(index1+1,index2-index1-1));
            list2.SetRange(0, list1.GetRange(index1 + 1, index2 - index1 - 1));


            foreach (int i in list2)
            {
                Console.Write(i + " ");
            }

            Console.ReadKey();
        }
    }

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务