unity游戏开发-C#语言基础篇(创建泛型集合)

class Program
    {
        static void Main(string[] args)
        {
            FanxingList<int> mylist = new FanxingList<int>();

            for (int i = 0; i < 10; i++)
            {
                mylist.Add(i);
            }
            mylist.Insert(1, 88);
            for (int i = 0; i < mylist.Count; i++)
            {
                Console.Write(mylist[i] + " ");
            }
            mylist.IndexOf(1, 2, 3, 4);
            Console.ReadKey();
        }
    }
 class FanxingList<T>
    {
        private T[] arr;
        private int count;

        public int Count
        {
            get { return this.count; }
            set { count = value; }
        }

        public int Capactiy
        {
            get { return arr.Length; }

        }


        public FanxingList()
        {

            arr = new T[] { };

        }

        public FanxingList(int length)
        {

            if (length >= 0)
            {
                arr = new T[length];
            }
            else
            {

                Console.WriteLine("不能为负数!");

            }

        }

        public void Add(T item)
        {

            if (this.count == this.Capactiy)
            {

                if (this.count == 0)//当等于0 的时候扩容,创建一个4的长度数组;
                {
                    arr = new T[4];
                }

                if (this.count == this.Capactiy && this.count != 0)//扩容操作
                {
                    T[] arrNew = new T[this.Capactiy * 2];//创建扩容数组
                    Array.Copy(arr, arrNew, this.count);//把原来数组copy到扩容后的数组;
                    arr = arrNew;
                }
            }
            arr[this.Count] = item;
            count++;
        }

        /// <summary>
        /// 设置索引器
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        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;
        }

        //插入
        public void Insert(int index, T item)
        {
            if (this.Count == 0)
            {
                arr = new T[4];
            }
            if (index >= 0 && index < 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 - 1; j >= index; j--)
                        {
                            arr[j + 1] = arr[j];

                        }
                        arr[index] = item;
                        count++;
                        break;
                    }
                }
            }
            else
            {
                for (int i = 0; i < this.count; i++)
                {

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

        }



        public int IndexOf(T item, int index, int _count)
        {   //有后面两个参数 
            for (int i = index; i <= index + _count; i++)
            {
                if (arr[i].Equals(item))
                {
                    return i;
                }
            }


            return -1;
        }

        public int IndexOf(T item)//重载 无后面参数
        {

            for (int i = 0; i < this.Count; i++)
            {
                if (arr[i].Equals(item))
                {
                    return i;

                }
            }
            return -1;
        }


        public int IndexOf(T item, params int[] Canshu)
        {

            if (Canshu.Length == 2)
            {
                for (int i = Canshu[0]; i <= Canshu[0] + Canshu[1]; i++)
                {
                    if (arr[i].Equals(item))
                    {
                        return i;
                    }
                }

            }
            else if (Canshu.Length == 0)
            {
                for (int i = 0; i < this.Count; i++)
                {
                    if (arr[i].Equals(item))
                    {
                        return i;

                    }

                }

            }
            else
            {
                Console.WriteLine("传入参数不正确!");

            }

            return -1;
        }




        public int LastIndexOf(T item)
        {
            int index = -1;
            for (int i = this.Count; i >= 0; i--)
            {
                if (arr[i].Equals(item))
                {
                    return i;
                }
            }


            return index;
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容