/// <summary>
        /// IterateOverList removes the 3rd item of a list and then
        /// writes it to console.
        /// </summary>
        /// <typeparam name="T">The type of iterator</typeparam>
        /// <param name="myList">The list to iterate over</param>
        public static void IterateOverList <T>(MyGenericList <T> myList)
        {
            //Get list iterator
            IMyEnumeratorInterface <T> myEnumerator = myList.GetMyEnumerator();

            //Remove the 3rd item in the list (if a 3rd item exists)
            if (myEnumerator.MoveNext())
            {
                if (myEnumerator.MoveNext())
                {
                    if (myEnumerator.MoveNext())
                    {
                        myEnumerator.Remove();                         //removes the third item
                    }
                }
            }

            //Resets to index -1
            myEnumerator.Reset();

            //Write all items in the list to the console
            //This is basically a foreach loop
            Console.WriteLine();
            while (myEnumerator.MoveNext())
            {
                Console.Write(myEnumerator.Current + " ");
            }
        }
        /// <summary>
        /// Main creates two MyGenericList objects, one of type int and one
        /// of type string. They are both initialised with values and then
        /// call the generic method IterateOverList for each which removes the 3rd
        /// value in the list and then writes the list to console.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            MyGenericList <int>    myIntegerList = new MyGenericList <int>();
            MyGenericList <string> myStringList  = new MyGenericList <string>();

            //MyGenericList add (MyGenericList is initialised to a size 10 array but
            //grows as the list grows)
            for (int i = 15; i > 0; i--)
            {
                myIntegerList.Add(i);
            }

            string[] aToQ = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
                              "o", "p", "q" };
            foreach (string letter in aToQ)
            {
                myStringList.Add(letter);
            }

            //Show the integer list with the 3rd item removed
            IterateOverList <int>(myIntegerList);
            Console.ReadKey();

            //Show the string list with the 3rd item removed
            IterateOverList <string>(myStringList);
            Console.ReadKey();
        }
Beispiel #3
0
 /// <summary>
 /// The constructor for MyGenericListIterator takes an instance of a
 /// MyGenericList(T) as an inparameter.  That list becomes the referenced
 /// list that is iterated over and the current position is set to -1.
 /// A MoveNext() is required to initialise and iterate to positon 0 so
 /// Current points to the first item at index 0.
 /// </summary>
 /// <param name="aList"></param>
 public MyGenericListIterator(MyGenericList <T> aList)
 {
     this.aList           = aList;
     this.currentPosition = -1;
 }