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;
                    }
                }

            }
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_31726339/article/details/83017536