1. 程式人生 > >2018-10-11泛型 建立自己的List類

2018-10-11泛型 建立自己的List類

自己寫了一個list類 簡單實現了

  1. CapaciTy獲取列表容量大小
  2. Count訪問元素個數
  3. Add()新增資料
  4. Insert()指定索引處新增資料
  5. [index]訪問元素(索引器)
  6. IndexOf()返回資料的下標
  7. LastIndexOf()從後往前返回資料的下標
  8. Sort()對列表中的資料進行從大到小排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 建立自己List
{
    class MyArray<T> where T:IComparable//可以比較T型別的資料的介面
    {
        private T[] array;
        private int capacity;
        private int count=0;

        //獲取列表容量的方法
        public int Capacity
        {
            get => capacity = array.Length;
        }
        public int Count
        { get => count; }

        /// <summary>
        /// 列表尾部插入資料
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            if (Capacity == Count)
            {
                if (capacity==0)
                { 
                    array = new T[4];
                }
                else
                {
                    var newArray = new T[capacity * 2];
                    Array.Copy(array, newArray, Count);
                    array = newArray;
                }
            }
            array[Count] = item;
            count++;
        }

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

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

        public T GetItem(int index)
        {
            if (index >= 0 && index <= count-1)
            {
                return array[index];
            }
            else
            {
            throw new Exception("索引越界");
            }
        }

        /// <summary>
        /// 建立索引器取值或設定值
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T this[int index]
        {
            get => GetItem(index);
            set
            {
                if (index >= 0 && index <= count - 1)
                {
                    array[index] = value;
                }
                else
                {
                    throw new Exception("索引越界");
                }
            }
        }
        /// <summary>
        /// 指定下標插入資料
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        public void InSert(int index, T item)
        {
            if (index >= 0 && index <= count - 1)
            {
                if (count == capacity)
                {
                    var newArray = new T[capacity * 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
            {
                throw new Exception("索引越界");
            }
        }

        public void RemoveAt(int index)
        {
            if (index >= 0 && index <= count - 1)
            {
                for (int i = index; i < count; i++)
                {
                    array[i] = array[i + 1];
                }

                count--;
            }
            else
            {
                throw new Exception("索引越界");
            }
        }
        /// <summary>
        /// 返回資料的索引
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int IndexOf(T item)
        {
            for (int i = 0; i < count; i++)
            {
                if (array[i].Equals(item))
                {
                    return i;
                }
                
            }
            return -1;
        }
        /// <summary>
        /// 從後往前返回資料的索引
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        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 i = 0; i < count-1; i++)
            {
                for (int j = i+1; j < count; j++)
                {
                    if (array[i].CompareTo(array[j]) > 0)
                    {
                        T temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }

            }
        }

    }
}