搜索
您的当前位置:首页正文

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();
        }
    }
Top