美文网首页
019_使用泛型和索引器来实现一个我们自己的集合类。

019_使用泛型和索引器来实现一个我们自己的集合类。

作者: 立秋i | 来源:发表于2018-05-21 15:53 被阅读0次

namespace _019_使用泛型和索引器来实现一个我们自己的集合类MyList { class MyList where T:IComparable

    {

        private T[] array;

        private int count=0;//表示当前添加的元素的个数

        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 (Capacity == 0)

                {

                    array = new T[4];//当数组长度为0的时候,创建一个长度为4的数组

                }

                else

                {

                    var newArray = new T[Capacity*2];//当长度不为0的时候,我们创建一个长度为原来2倍的数组

                    Array.Copy(array,newArray,Count);//把旧数组中的元素复制到新的数组中 , 复制前count个  array-->newArray

                    array = newArray;

                }

            }

            array[Count] = item;

            count++;//元素个数自增

        }

        public T GetItem(int index)

        {

            if (index >= 0 && index <= count - 1)

            {

                return array[index];

            }

            else

            {

                //Console.WriteLine("所以超出了范围");

                throw new Exception("索引超出了范围");

            }

        }

        public T this[int index]

        {

            get//当我们通过索引器取值的时候,会调用get块

            { return GetItem(index); }

            set//当我们通过索引器设置值的时候,会调用set块

            {

                if (index >= 0 && index <= count - 1)

                {

                    array[index] = value;

                } else {

                    //Console.WriteLine("所以超出了范围");

                    throw new Exception("索引超出了范围");

                }

            }

        }

        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];//把i位置的值放在后面,就是向后移动一个单位

                }

                array[index] = item;

                count++;

            }

            else

            {

                throw new Exception("所以超出范围");

            }

        }

        public void RemoveAt(int index)

        {

            if (index >= 0 && index <= count - 1)

            {

                for (int i = index + 1; i < count; i++)

                {

                    array[i - 1] = array[i];

                }

                count--;

            }

            else

            {

                throw new Exception("所以超出范围");

            }

        }

        public int IndexOf(T item)

        {

            for (int i = 0; i < count; i++)

            {

                if (array[i].Equals(item))

                {

                    return i;

                }

            }

            return -1;

        }

        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 j = 0; j < Count-1; j++)

            {

                for (int i = 0; i < Count - 1 - j; i++) {

                    if (array[i].CompareTo(array[i + 1]) > 0) {

                        T temp = array[i];

                        array[i] = array[i + 1];

                        array[i + 1] = temp;

                    }

                }

            }

        }

    }

}

———————————————————————————————————————————————————————————————

namespace _019_使用泛型和索引器来实现一个我们自己的集合类MyList { class Program { static void Main(string[] args) { MyListmylist = new MyList();

            mylist.Add(234);

            mylist.Add(14);

            mylist.Add(24);

            mylist.Add(24);

            mylist.Add(90);

            mylist.Add(274);

            //mylist[index]

            mylist.Insert(1,2);

            //mylist.RemoveAt(-1);

            mylist[0] = 100;//通过索引器 设置值

            for (int i = 0; i < mylist.Count; i++)

            {

                //Console.WriteLine(mylist.GetItem(i));

                Console.WriteLine(mylist[i]);//通过索引器 取值

            }

            Console.WriteLine(mylist.IndexOf(24));

            Console.WriteLine(mylist.LastIndexOf(24));

            mylist.Sort();

            Console.WriteLine();

            for (int i = 0; i < mylist.Count; i++) {

                //Console.WriteLine(mylist.GetItem(i));

                Console.WriteLine(mylist[i]);//通过索引器 取值

            }

            Console.ReadKey();

        }

    }

}

相关文章

  • 019_使用泛型和索引器来实现一个我们自己的集合类。

    namespace _019_使用泛型和索引器来实现一个我们自己的集合类MyList { class MyL...

  • 集合

    集合 Java集合框架 将集合的接口和实现分离 Collection接口 迭代器 泛型使用方法 集合框架中的接口 ...

  • 泛型

    泛型的使用 jdk 5.0新增的特性 在集合中使用泛型 ① 集合接口或集合类在jdk5.0时都修改为带泛型的结构。...

  • 泛型(jdk5.0新特性)

    在集合中使用泛型 1、集合接口或集合类在jdk5.0时都修改为带泛型的结构2、在实例化集合类时,可以指明具体的泛型...

  • Kotlin-泛型和委托

    泛型 泛型的使用是为了程序有更好的扩展性。泛型类和泛型方法 泛型的高级特性java的泛型是通过类型擦除机制来实现的...

  • 集合泛型处理时处理subsequence的技巧

    我们这几天一直在探究集合在泛型化时所遇到的问题。在另外一个内容集合索引与泛型中,我们讨论的是在集合泛型化处理的时候...

  • Java泛型

    本文介绍的知识点 泛型是什么? 泛型的使用在反射中使用泛型在集合类中使用泛型 关于泛型擦除如何理解?如何避免泛型擦...

  • 8.泛型

    1.泛型在集合中的使用(掌握) //集合类或接口,在声明的时候,使用了泛型。那么我们就可以在实例化的时候,声明具体...

  • 四、Java高级--1、泛型

    泛型定义:数据类型参数化,提前定义好集合中放入什么类型集合框架中没使用泛型和使用泛型的比较 泛型规则和限制1、泛型...

  • iOS 泛型 ObjectType 协变 __covariant

    泛型使用场景: 在集合(数组、字典、NSSet)中使用泛型比较常见。 当声明一个类,类里面的某些属性的类型不确定,...

网友评论

      本文标题:019_使用泛型和索引器来实现一个我们自己的集合类。

      本文链接:https://www.haomeiwen.com/subject/clzfjftx.html