利用C#创建一个我们自己的列表

来源:互联网 发布:网络音响怎么布线 编辑:程序博客网 时间:2024/06/03 13:44

通过C#创建一个列表,实现索引器,构造器,添加,删除,排序等功能。

 class MyList<T> where T:IComparable    {        private T[] array;        private int count;        public MyList(int size)        {            if (size >= 0)            {                array = new T[size];            }        }        public MyList()        {            array = new T[0];        }        public int Capacity//获取容量        {            get { return array.Length; }        }        public int Count { get { return count; } }        public void Add(T item)//添加元素        {            if (count == Capacity)//如果装满了            {                if (array.Length == 0)//如果初始容量仍是0                {                    array = new T[4];                }                else                {                    T[] tempArray = new T[2 * count];                    Array.Copy(array, tempArray, count);                    array = tempArray;                }            }            array[count] = item;            count++;        }        public T GetItem(int index)//获取元素        {            if (index >= 0 && index <= count - 1)            {                return array[index];            }            else            {                throw new Exception("索引超出范围!");//抛出异常或者返回默认值                //return default(T);            }        }        public T this[int index]//添加索引器        {            get { return GetItem(index); }            set            {                if (index >= 0 && index <= count - 1)                {                    array[index] = value;                }                else                {                    throw new Exception("索引超出范围!");                }            }        }        #region        public void Insert(int index, T item)//指定位置添加元素        {            if (index >= 0 && index <= count - 1)            {                if (count == Capacity)//如果装满了                {                    T[] tempArray = new T[2 * count];                    Array.Copy(array, tempArray, count);                    array = tempArray;                }                for (int i = count; i > index; i--)                {                    array[i] = array[i - 1];                }                array[index] = item;                count++;            }            else            {                throw new Exception("索引超出范围!");            }        }        #endregion        public void RemoveAt(int index)//移除指定位置元素        {            if (index > 0 && index <= count - 1)            {                for (int i = index; i < count-1; i++)                {                    array[index] = array[index + 1];                }                count--;            }            else            {                throw new Exception("索引超出范围!");            }        }        public void Sort()//通过冒泡排序进行排序        {            bool isSwap = false;            for (int i = 0; i < count-1; i++)            {                for (int j = 0; j <count-i-1 ; j++)                {                    if (array[j].CompareTo(array[j+1])>0)                    {                        T temp = array[j];                        array[j] = array[j+1];                        array[j+1] = temp;                        isSwap = true;                    }                }                if (!isSwap)                {                    break;                }            }        }    }
阅读全文
0 0
原创粉丝点击