自己写的List类

 我写的List,还没100%完成,有空修改完补贴新代码,修改一下T的类型就可以模拟范型的强类型效果。

例如

using T = System.Byte;

using T = System.Int32;

 

为什么要自己写?因为有时候系统的类不符合要求,例如性能,例如功能.....

using System;
using System.Collections;

using T = System.Byte;

public class TList
{
    const int DEFAULT_CAPACITY = 4;
    static readonly T[] emptyArray = new T[0];
    private object syncRoot;
    private T[] items;
    private int size;

    public TList()
    {
        this.items = TList.emptyArray;
    }
    public TList(ICollection c)
    {
        if (c == null) throw new ArgumentNullException("c", "ArgumentNull_Collection");
        this.items = new T[c.Count];
        this.AddRange(c);
    }
    public TList(int capacity)
    {
        if (capacity < 0) throw new ArgumentOutOfRangeException("capacity", "ArgumentOutOfRange_MustBeNonNegNum", "capacity");
        this.items = new T[capacity];
    }

    private void ensureCapacity(int min)
    {
        if (this.items.Length < min)
        {
            int len = (this.items.Length == 0) ? 4 : (this.items.Length << 1);
            if (len < min) len = min;
            this.Capacity = len;
        }
    }

    public int Capacity
    {
        get { return this.items.Length; }
        set
        {
            if (value != this.items.Length)
            {
                if (value < this.size) throw new ArgumentOutOfRangeException("value", "ArgumentOutOfRange_SmallCapacity");
                if (value > 0)
                {
                    T[] array = new T[value];
                    if (this.size > 0) Array.Copy(this.items, 0, array, 0, this.size);
                    this.items = array;
                }
                else
                    this.items = new T[4];
            }
        }
    }

    public void InsertRange(int index, ICollection c)
    {
        if (c == null) throw new ArgumentNullException("c", "ArgumentNull_Collection");
        if ((index < 0) || (index > this.size)) throw new ArgumentOutOfRangeException("index", "ArgumentOutOfRange_Index");
        int count = c.Count;
        if (count > 0)
        {
            this.ensureCapacity(this.size + count);
            if (index < this.size) Array.Copy(this.items, index, this.items, index + count, this.size - index);
            T[] array = new T[count];
            c.CopyTo(array, 0);
            array.CopyTo(this.items, index);
            this.size += count;
        }
    }

    public void AddRange(ICollection c)
    {
        this.InsertRange(this.size, c);
    }

    public int Add(T value)
    {
        if (this.size == this.items.Length)
            this.ensureCapacity(this.size + 1);
        this.items[this.size] = value;
        return this.size++;
    }

    public void Clear()
    {
        Array.Clear(this.items, 0, this.size);
        this.size = 0;
    }

    public bool Contains(T value)
    {
        for (int i = 0; i < this.size; i++)
            if (this.items[i] == value)
                return true;
        return false;
    }

    public int IndexOf(T value)
    {
        return Array.IndexOf(this.items, value, 0, this.size);
    }

    public void Insert(int index, T value)
    {
        if ((index < 0) || (index > this.size)) throw new ArgumentOutOfRangeException("index", "ArgumentOutOfRange_ArrayListInsert");
        if (this.size == this.items.Length)
            this.ensureCapacity(this.size + 1);
        if (index < this.size)
            Array.Copy(this.items, index, this.items, index + 1, this.size - index);
        this.items[index] = value;
        this.size++;
    }

    public bool IsFixedSize { get { return false; } }

    public bool IsReadOnly { get { return false; } }

    public void Remove(T value)
    {
        int i = this.IndexOf(value);
        if (i >= 0) this.RemoveAt(i);
    }

    public void RemoveAt(int index)
    {
        if ((index < 0) || (index >= this.size)) throw new ArgumentOutOfRangeException("index", "ArgumentOutOfRange_Index");
        this.size--;
        if (index < this.size)
            Array.Copy(this.items, index + 1, this.items, index, this.size - index);
        this.items[this.size] = 0;    //这里需要吗?
    }

    public T this[int index]
    {
        get
        {
            if ((index < 0) || (index >= this.size)) throw new ArgumentOutOfRangeException("index", "ArgumentOutOfRange_Index");
            return this.items[index];
        }
        set
        {
            if ((index < 0) || (index >= this.size)) throw new ArgumentOutOfRangeException("index", "ArgumentOutOfRange_Index");
            this.items[index] = value;
        }
    }

    public void CopyTo(Array array, int index)
    {
        if ((array != null) && (array.Rank != 1)) throw new ArgumentException("Arg_RankMultiDimNotSupported");
        Array.Copy(this.items, 0, array, index, this.size);
    }

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

    public bool IsSynchronized { get { return false; } }

    public object SyncRoot
    {
        get
        {
            if (this.syncRoot == null)
                System.Threading.Interlocked.CompareExchange(ref this.syncRoot, new object(), null);
            return this.syncRoot;
        }
    }

    public IEnumerator GetEnumerator()
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值