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

namespace 自定义集合类MyList
{
class MyList where T :IComparable
{
private T[] array;
private int count = 0;

    public MyList(int size)
    {
        if (size > 0)
            array = new T[size];
    }

    public MyList()
    {
        array = new T[0];
    }

    public int Capecity
    {
        get { return array.Length; }
    }

    public int Count
    {
        get { return count; }
    }

    public void Add(T item)
    {
        if (Count == Capecity)//判断元素个数与数组容量
        {
            if (Capecity == 0)
                array = new T[4];
            else
            {
                var newarray = new T[Capecity * 2];
                Array.Copy(array, newarray, Count);
                array = newarray;
            }
        }
        array[Count] = item;
        count++;
    }

    public T GetItem(int index)
    {
        if (index >= 0 && index <= count-1)
        {
            return array[index];
        }
        else
        {
            Console.WriteLine("Index outof range");
            throw new Exception("索引超出范围");
        }

    }

    //索引器,属性
    public T this[int index]
    {
        get { return GetItem(index); }
        set
        {
            if (index >= 0 && index <= count-1)
            {
                array[index] = value;
            }
            else
            {
                Console.WriteLine("Index outof range");
                throw new Exception("索引超出范围");
            }
        }
    }

    public void Insert(int index, T item)
    {
        if (index >= 0 && index <= count-1)
        {
            if (Count  == Capecity )//容量不足进行扩容
            {
                var newArray = new T[Capecity * 2];
                Array.Copy(array, newArray, count);
                array = newArray;
            }
            for (int i = count -1; i >= index ; i--)
            {
                array[i+1] = array[i];
            }
            array[index] = item;
            count++;
        }
        else
        {
            Console.WriteLine("Index outof range");
            throw new Exception("索引超出范围");
        }

    }
    
    public void RemoveAt(int index)
    {
        if (index >= 0 && index <= count-1)
        {
            for (int i = index +1; i < count ; i++)
            {
                array[i - 1] = array[i];
            }
            count--;
        }
        else
        {
            throw new Exception("索引超出范围");
        }
    }

    public int IndexOf(T item)
    {
        for (int i = 0; i < count ; i++)
        {
            if (array[i].Equals(item))
            {
                return i;
            }
        }
        return -1;
    }

    public int LastIndexOf(T item)
    {
        for (int i = count -1; i >=0; i--)
        {
            if (array [i].Equals (item ))
            {
                return i;
            }
        }
        return -1;
    }

    public void Sort()
    {
        for (int j = 0; j < Count -1; j++)
        {
            for (int i = 0; i < Count-1-j; i++)
            {
                if (array[i].CompareTo(array[i + 1]) > 0)//返回值大于0是后面的铋前面的大
                {
                    //打的元素向后排
                    T temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
        }
        
    }

}

}