using System;
using System.Collections;
namespace P7_9
{
    class PList<T> : IEnumerable 	//ʵִ˽ӿ
    {
        T[] objs = new T[4];    	//飬ʼĸԪ
        int count = 0;          	//ǰԪ
        //
        public T this[int index]
        {
            get
            {
                if (index > 0 && index < count)
                {
                    return objs[index];
                }
                return default(T);
            }
            set
            {
                if (index > 0 && index < count)
                {
                    objs[index] = value;
                }
            }
        }
        /// <summary>
        /// Ԫ
        /// </summary>
        /// <param name="t"></param>
        public void Add(T t)
        {
            //Ԫռ
            if (count == objs.Length)
            {
                T[] tmp = objs;
                objs = new T[count + 2];
                tmp.CopyTo(objs, 0);
            }
            objs[count++] = t;
        }
        /// <summary>
        /// ɾԪ
        /// </summary>
        /// <param name="index">Ԫ</param>
        public void Remove(int index)
        {
            if (index >= count || index < 0)
            {
                return;
            }
            for (int i = index; i < count - 1; i++)
            {
                objs[i] = objs[i + 1];
            }
        }
        //ʵֽӿڵķForeach
        public IEnumerator GetEnumerator()
        {
            return objs.GetEnumerator();
        }
    }
class Program
    {
        static void Main(string[] args)
        {
            PList<int> pl = new PList<int>();
            pl.Add(13);
            pl.Add(543);
            pl.Add(123);
            pl.Add(111);
            pl.Remove(1);
            foreach (int i in pl)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine(pl[1]);
            Console.ReadLine();
        }
    }
    
}
