《C#高級編程》第9章:集合,ArrayList,Stack,Queue,SortedList

ArrayList

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace MyArrayList
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            Console.WriteLine(al.Capacity);
            Console.WriteLine(al.Count);


            al.Add("al1");
            al.Add("al2");
            al.Add("al3");

            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();

            string[] str = new string[] { "str1","str2"};
            al.AddRange(str);

            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();

            al.RemoveAt(1);

            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();

            al.RemoveRange(2, 2);
            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();
          


        }
    }
}
 

//******************************************

Stack

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace MyStack
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack sk = new Stack();
            sk.Push("aaa");
            sk.Push("bbb");
            sk.Push("ccc");

            foreach (string item in sk)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("/nItem Pop():" + sk.Pop().ToString());
            Console.WriteLine("/nItem Peek():" + sk.Peek().ToString());
            Console.WriteLine("Second iteration: ");

            foreach (string item in sk)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("");
            Console.WriteLine("******");
            Console.WriteLine("");


            // Creates and initializes the source Stack.
            Stack mySourceQ = new Stack();
            mySourceQ.Push("barn");
            mySourceQ.Push("the");
            mySourceQ.Push("in");
            mySourceQ.Push("cats");
            mySourceQ.Push("napping");
            mySourceQ.Push("three");

            // Creates and initializes the one-dimensional target Array.
            Array myTargetArray = Array.CreateInstance(typeof(String), 15);
            myTargetArray.SetValue("The", 0);
            myTargetArray.SetValue("quick", 1);
            myTargetArray.SetValue("brown", 2);
            myTargetArray.SetValue("fox", 3);
            myTargetArray.SetValue("jumped", 4);
            myTargetArray.SetValue("over", 5);
            myTargetArray.SetValue("the", 6);
            myTargetArray.SetValue("lazy", 7);
            myTargetArray.SetValue("dog", 8);

            // Displays the values of the target Array.
            Console.WriteLine("The target Array contains the following (before and after copying):");
            PrintValues(myTargetArray, ' ');

            // Copies the entire source Stack to the target Array, starting at index 6.
            mySourceQ.CopyTo(myTargetArray, 6);

            // Displays the values of the target Array.
            PrintValues(myTargetArray, ' ');

            // Copies the entire source Stack to a new standard array.
            Object[] myStandardArray = mySourceQ.ToArray();

            // Displays the values of the new standard array.
            Console.WriteLine("The new standard array contains the following:");
            PrintValues(myStandardArray, ' ');

        }


        public static void PrintValues(Array myArr, char mySeparator)
        {
            foreach (Object myObj in myArr)
            {
                Console.Write("{0}{1}", mySeparator, myObj);
            }
            Console.WriteLine();
        }
    }
      
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章