c#自定义泛型列表

来源:互联网 发布:mysql insert 编辑:程序博客网 时间:2024/06/08 12:35
public class MyException:Exception
{
    public  MyException (string msg) : base (msg)
    {
    }

}



public class MyArrayList<T>
{
    T[] list;
    int capacity = 0;
    int count = 0;
    bool isFixedSize = false;

    public MyArrayList ()
    {
    }

    public MyArrayList (int capacity)
    {
        this.capacity = capacity;
        list = new T[capacity];
        isFixedSize = true;
    }

    public int Count {
        get{ return this.count; }
    }

    public int Capacity {
        get{ return capacity; }
    }

    public bool IsFixedSize ()
    {
        return isFixedSize;
    }
    //索引器
    public T this [int index] {
        get { 
            return list [index];
        }
        set {
            list [index] = value;
        }
    }

    public void Add (T value)
    {
        if (list == null) {
            list = new T[4];
            capacity = 4;
        } 
        if (count >= capacity) {
            T[] temp = list;
            list = new T[capacity *= 2];
            for (int i = 0; i < count; i++) {
                list [i] = temp [i];
            }
        } 
        list [count++] = value;
    }

    public void Sort ()
    {
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count - i - 1; j++) {
                if (list [i] is Person) {
                    IMyComparable obj1 = (IMyComparable)list [j];
                    IMyComparable obj2 = (IMyComparable)list [j + 1];
                    if (obj1.ComparaTo (obj2) > 0) {
                        T temp = list [j];
                        list [j] = list [j + 1];
                        list [j + 1] = temp;
                    }
                } else if (list [i] is IComparable) {
                    IComparable obj1 = (IComparable)list [j];
                    IComparable obj2 = (IComparable)list [j + 1];
                    if (obj1.CompareTo (obj2) > 0) {
                        T temp = list [j];
                        list [j] = list [j + 1];
                        list [j + 1] = temp;
                    }
                } else {
                    throw new MyException ("不能识别");
                }
            }
        }
    }

    public int BinarySearch (T obj)
    {
        if (obj is Person) {
            Person    value = (Person)(object)obj;
            if (count == 0 || value.Age < ((Person)(object)list [0]).Age) {
                return -1;
            }
            int start = 0;
            int end = list.Length;
            int middle = (start + end) / 2;
            while (start < end && (Person)(object)list [middle] != value) {
                if (((Person)(object)list [middle]).ComparaTo (value) < 0) {
                    start = middle + 1;
                }
                if (((Person)(object)list [middle]).ComparaTo (value) > 0) {
                    end = middle - 1;
                }
                middle = (start + end) / 2;
            }
            if ((Person)(object)list [middle] != value) {
                return -1;
            } else {
                return middle;
            }
        } else if (obj is IComparable) {
            IComparable    value = (IComparable)obj;
            if (count == 0 || value.CompareTo ((IComparable)list [0]) < 0) {
                return -1;
            }
            int start = 0;
            int end = list.Length;
            int middle = (start + end) / 2;
            while (start < end && (0 != value.CompareTo ((IComparable)list [middle]))) {
                if (value.CompareTo ((IComparable)list [middle]) > 0) {
                    start = middle + 1;
                }
                if (value.CompareTo ((IComparable)list [middle]) < 0) {
                    end = middle - 1;
                }
                middle = (start + end) / 2;
            }
            if (value.CompareTo ((IComparable)list [middle]) != 0) {
                return -1;
            } else {
                return middle;
            }
        }
        throw new MyException ("不能识别此类型");
    }


    public void Clear ()
    {
        count = 0;
        list = new T[capacity];
    }

    public bool Contains (T value)
    {
        foreach (T obj in list) {
            if ((object)obj == ((object)value)) {
                return true;
            }
        }
        return false;
    }

    public int IndexOf (T value)
    {
        int index = -1;
        for (int i = 0; i < count; i++) {
            T obj = list [i];
            if (obj.Equals (value)) {
                index = i;
            }
        }
        return index;
        ;
    }

    public void Insert (int index, T value)
    {
        if (index > count + 1) {
            throw new MyException ("下标超出范围");
        }
        if (list == null) {
            capacity = 4;
        }
        if (count + 1 >= capacity) {
            capacity *= 2;
        }
        T[] temp = list;
        list = new T[capacity];

        if (temp != null) {
            for (int i = 0; i <= count; i++) {
                if (i < index) {
                    list [i] = temp [i];
                } 
                if (i == index) {        
                    list [i] = value;
                } 
                if (i > index) {
                    list [i] = temp [i - 1];
                }
            }
            count++;
        }
    }

    public void Remove (T value)
    {
        T[] temp = list;
        list = new T[capacity];
        int j = 0;
        bool isSucced = false;
        for (int i = 0; i < count; i++) {
            T obj = temp [i];
            if (obj.Equals (value) && isSucced == false) {
                isSucced = true;
            } else {
                list [j] = obj;
                j++;
            }
        }
        if (isSucced) {
            count--;
        } else {
            throw new MyException ("未找到要移除元素");
        }
    }

    public void RemoveAt (int index)
    {
        if (index >= count || index < 0) {
            throw new MyException ("下标超出索引范围");
        }
        T[] temp = list;
        list = new T[capacity];
        int item = 0;
        for (int i = 0; i < count; i++) {
            if (i != index) {
                list [item] = temp [i];
                item++;
            }
        }
        count--;
    }

    public void Reverse ()
    {
        for (int i = 0; i < count / 2; i++) {
            T temp = list [i];
            list [i] = list [count - 1 - i];
            list [count - 1 - i] = temp;
        }
    }

    public T[] ToArray ()
    {
        T[] temp = new T[count];
        for (int i = 0; i < count; i++) {
            temp [i] = list [i];
        }
        return temp;
    }

    public override  string ToString ()
    {
        string str = "";
        if (count == 0) {
            return "{}";
        }
        for (int i = 0; i < count; i++) {
            if (i == 0) {
                str += ("{" + list [i] + ",");
            } else if (i == count - 1) {
                str += (list [i] + "}");
            } else {
                str += list [i] + ",";
            }
        }
        return String.Format ("{0} count{1} capacity{2}", str, count, capacity);
    }

    public void TrimToSize ()
    {
        if (capacity > count) {
            capacity = count;
        }
        T[] temp = list;
        list = new T[capacity];
        for (int i = 0; i < count; i++) {
            list [i] = temp [i];
        }
    }
}


public interface IMyComparable
{
    int ComparaTo (object obj);
}

public  class Person: IMyComparable
{
    int age;

    public int Age {
        get{ return age; }
        set{ age = value; }
    }

    public Person ()
    {
    }

    public Person (int age)
    {
        this.age = age;
    }

    public override string ToString ()
    {
        return string.Format ("[Person: Age={0}]", Age);
    }

    public    int ComparaTo (object obj)
    {
        if (obj is Person) {
            Person other = (Person)obj;
            if (age > other.age)
                return 1;
            else if (age < other.age)
                return -1;
            else if (age == other.age)
                return 0;
        }
        throw new MyException ("比较识别不是Person的类型");
    }

    public    static int Compara (object obj1, object obj2)
    {
        if (obj1 is Person && obj2 is Person) {
            Person p1 = (Person)obj1;
            Person p2 = (Person)obj2;
            if (p1.Age > p2.Age)
                return 1;
            else if (p1.Age < p2.Age)
                return -1;
            else if (p1.Age == p2.Age)
                return 0;    
        }
        throw new MyException ("比较识别不是Person的类型");
    }
}




0 0