Unity3D开发-C#语言进阶篇(创建集合类)

  class Program
    {
        static void Main(string[] args)
        {
            //创建 一个 集合类 封装
            ListClass myList = new ListClass();
            for (int i = 0; i < 10; i++)
            {
                myList.Add(i);
            }



            for (int i = 0; i < myList.Count; i++)
            {
                Console.WriteLine(myList[i]);
            }

            Console.ReadKey();
        }
    }
 class ListClass
    {
        private T[] arr; //字段;

        private int count;

        public int Count//属性
        {
            get { return count; }
            set { count = value; }
        }

        private int capactiy;

        public int Capactiy
        {
            get { return capactiy; }
            set { capactiy = value; }
        }

        //构造函数
        public ListClass()
        {

            arr = new T[] { };

        }

        public ListClass(int length)
        {
            if (length >= 0)
            {
                arr = new T[length];
            }
            else
            {
                Console.WriteLine("长度不能小于零!");
            }


        }

        //添加方法
        public void Add(T str)
        {

            if (count == this.Capactiy)
            {
                if (count == 0)
                {
                    arr = new T[4];
                }
                if (count == this.Capactiy && this.Count != 0)
                {
                    T[] arrNew = new T[this.Capactiy * 2];
                    Array.Copy(arr, arrNew, this.Count);
                    arr = arrNew;
                }
            }

            arr[count] = str;
            this.Count++;
        }


        //设置索引器

        public T this[int index]
        {

            get { return this.GetItem(index); }
            set
            {

                if (index >= 0 && index < this.Capactiy)
                {
                    arr[index] = value;
                }
                else
                {
                    throw new Exception("索引产出范围!");

                }
            }

        }


        private T GetItem(int index)
        {
            if (index >= 0 && index < this.Capactiy)
            {
                return arr[index];
            }
            else
            {
                throw new Exception("索引产出范围!");

            }
        }

        //插入一个元素

        public void Insert(int index, T item)
        {
            if (this.count + 1 > this.Capactiy)
            {
                T[] arrNew = new T[this.Capactiy * 2];//创建扩容数组
                Array.Copy(arr, arrNew, this.count);//把原来数组copy到扩容后的数组;
                arr = arrNew;

                for (int i = 0; i < this.count; i++)
                {

                    if (i >= index)
                    {
                        for (int j = this.count; j >= index; j--)
                        {
                            arr[j] = arr[j - 1];

                        }
                        arr[index] = item;
                        count++;
                        break;



                    }

                }



            }
            else
            {
                for (int i = 0; i < this.count; i++)
                {

                    if (i >= index)
                    {
                        for (int j = this.count; j >= index; j--)
                        {
                            arr[j] = arr[j - 1];

                        }
                        this.count++;
                        arr[index] = item;
                        break;
                    }

                }
            }

        }

        //指定下标移除元素
        public void RemoveAt(int index)
        {
            for (int i = 0; i < this.count; i++)
            {
                if (i == index)
                {
                    for (int j = index; j < this.count; j++)
                    {
                        arr[j] = arr[j + 1];
                    }
                    this.count--;
                    break;

                }
            }
        }

        //指定元素寻找第一次出现的下标
        public int IndexOf(T str)
        {
            int index = -1;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].Equals(str))//判断是否相等
                {
                    index = i;
                }
            }
            return index;
        }



        //反转
        public void Reverse()
        {
            T[] arrTemp = new T[this.count];
            for (int i = 0; i < this.count; i++)
            {
                arrTemp[i] = arr[this.count - 1 - i];
            }
            arr = arrTemp;
        }
    }

你可能感兴趣的:(Unity3D开发-C#语言进阶篇(创建集合类))