自己写的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.");
    }
}

 

在Python,我们可以使用import语句来导入自己。根据引用\[1\]的示例代码,我们可以看到在pv.py文件定义了一个名为test的。要在另一个文件导入这个,我们可以使用以下方法: 1. 内部导入:如果我们想在同一个文件导入自己,可以使用import语句直接导入。例如,在test.py文件,我们可以使用import pv来导入pv.py文件的内容。然后,我们可以创建test的实例并访问其属性和方法。 2. 外部导入:如果我们想在不同的文件导入自己,我们需要确保Python解释器能够找到我们的模块。根据引用\[2\]和引用\[3\]的说明,我们可以使用sys模块来告诉Python解释器我们的模块的位置。我们可以在导入之前使用sys.path.append()方法将模块所在的路径添加到sys.path列表。然后,我们可以使用import语句导入模块,并创建的实例并访问其属性和方法。 综上所述,要在Python导入自己,可以使用import语句,并根据需要选择内部导入或外部导入的方法。 #### 引用[.reference_title] - *1* [python 自己,内外部import导入使用](https://blog.csdn.net/qq_34401858/article/details/128296309)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python import自己创建的.py文件-python 将自己的py文件作为模块导入](https://blog.csdn.net/weixin_39888268/article/details/109622050)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值