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();
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容