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

实现以下功能:

1.Capacity获取容量大小 2.Add()方法添加元素 3.Insert()方法插入元素 4.[index]访问元素(索引器) 5.Count属性访问元素个数 6.RemoveAt()方法移除指定位置的元素 7.IndexOf()方法取得一个元素所在列表中的索引位置,失败返回-1 8.LastIndexOf()上面的方法是从前往后搜索,这个是从后往前搜索,搜索到满足条件的就停止,失败返回-1 9.Sort()对列表中是元素进行从小到大排序

using System;

namespace MyList
{
    //为了能使用比较方法,需要实现IComparable接口
    class MyList<T> where T:IComparable {

        private T[] array;
        private int count;

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

        private void checkCapacity() {
            if (Count == Capacity) {
                if (Capacity == 0) {
                    array = new T[4];
                } else {
                    var newArray = new T[Capacity * 2];
                    Array.Copy (array, newArray, Count);
                    array = newArray;
                }
            } 
        }

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

        // 构建一个索引器, 通过索引器取元素
        public T this[int index] {
            get { return GetItem(index); }
            set {
                if (index >= 0 && index <= count - 1) {
                    array [index] = value;
                } else {
                    throw new Exception ("索引超出范围");
                }
            }
        }

        public void Add(T item) {
            checkCapacity ();
            array [Count] = item;
            count++;
        }

        public void Insert(int index, T item) {
            checkCapacity ();
            if (index >= 0 && index <= count - 1) {
                for (var 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 (var i = index + 1; i < count; i++) {
                    array [i - 1] = array [i];
                }
                count--;
            } else {
                throw new Exception ("索引超出范围");
            }
        }

        public int IndexOf(T item) {
            for (var i = 0; i < count; i++) {
                if (array [i].Equals (item)) {
                    return i;
                }
            }
            return -1;
        }

        public int LastIndexOf(T item) {
            for (var i = count - 1; i >= 0; i--) {
                if (array [i].Equals (item)) {
                    return i;
                }
            }
            return -1;
        }

        public void Sort() {
            for (var i = 0; i < count - 1; i++) {
                for (var j = 0; j < count - 1 - i; j++) {
                    if (array [j].CompareTo(array[j + 1]) > 0) {
                        T temp = array [j];
                        array [j] = array [j + 1];
                        array [j + 1] = temp;
                    }
                }
            }
        }
    }

    class MainClass
    {
        public static void Main (string[] args)
        {
            MyList<int> myList = new MyList<int> ();
            myList.Add (123);
            myList.Add (333);
            myList.Add (789);
            myList.Add (666);
            myList.Add (777);
            myList [0] = 999;
            for(var i = 0; i < myList.Count; i++) {
                Console.WriteLine(myList[i]);
            }

            Console.WriteLine ("Capacity:" + myList.Capacity);
            Console.WriteLine ("Count:" + myList.Count);

            Console.WriteLine ("----------------");

            myList.Insert (3, 555);

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

            Console.WriteLine ("Capacity:" + myList.Capacity);
            Console.WriteLine ("Count:" + myList.Count);

            Console.WriteLine ("----------------");

            myList.RemoveAt (1);

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

            Console.WriteLine ("Capacity:" + myList.Capacity);
            Console.WriteLine ("Count:" + myList.Count);

            Console.WriteLine ("----------------");

            myList.Sort ();

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

            Console.WriteLine ("Capacity:" + myList.Capacity);
            Console.WriteLine ("Count:" + myList.Count);

            Console.WriteLine ("----------------");

            Console.WriteLine ("index of 555: " + myList.IndexOf (555));
            Console.WriteLine ("index of 555: " + myList.LastIndexOf (555));
        }
    }
}

输出:

999
333
789
666
777
Capacity:8
Count:5
----------------
999
333
789
555
666
777
Capacity:8
Count:6
----------------
999
789
555
666
777
Capacity:8
Count:5
----------------
555
666
777
789
999
Capacity:8
Count:5
----------------
index of 555: 0
index of 555: 0

results matching ""

    No results matching ""