C#基础,制作一个有存储和删减元素和数组的int型集合

class List
    {
        //目标集合
        int[] numArr;
        //接收集合再赋值时的数组
        int[] arr;   
        //集合能容纳的最大长度
        private int Capcity;
        //集合当前所有元素的个数
        private int count;
        //让外界可以查看集合目前的元素个数
        public int Count
        {
            get
            {
                return count;
            }
        }
        //让外界可以查看当前集合可以容纳的最大元素个数
        public int Capcity1 { get => Capcity; set => Capcity = value; }
        //集合的初始化
        public List()
        {
            Capcity1 = 0;
            count = 0;
            numArr = new int[Capcity1];
        }
        //集合的下标索引器
        public int this[int index]
        {
            get
            {
                if (index < 0)
                {
                    return -1;
                }
                else if (index > count - 1)
                {
                    return -1;
                }
                return numArr[index];
            }
            set
            {
                numArr[index] = value;
            }
        }
        //添加单个元素的方法
        public void Add(int num)
        {
            count++;
            if (Capcity1 == 0)
            {
                Capcity1 = Capcity1 + 1;
                arr = new int[Capcity1];

                for (int i = 0; i < numArr.Length; i++)
                {
                    arr[i] = numArr[i];
                }
                numArr = new int[Capcity1];
                for (int i = 0; i < Capcity1; i++)
                {
                    numArr[i] = arr[i];
                }
            }
            else if (count >= Capcity1 && Capcity1 != 0)
            {
                Capcity1 *= 4;
                arr = new int[Capcity1];
                for (int i = 0; i < numArr.Length; i++)
            {
                arr[i] = numArr[i];
            }
            numArr = new int[Capcity1];
            for (int i = 0; i < Capcity1 / 4; i++)
            {
                numArr[i] = arr[i];
                
            }
        }

        for (int i = count - 1; i < count; i++)
        {
            numArr[i] = num;
        }
    }
    //获取要删除元素原本在集合中的第一个下标
    public int _index;
    //删除集合中的特定元素的第一个匹配项
    public void Remove(int num)
    {
        int r = 0;
        for (int i = 0; i < count; i++)
        {
            if (numArr[i] != num)
            {
                r++;
            }
        }
        if (count == 0||r>=count)
        {                
            Console.WriteLine("false");
        }
        else if (count >0&&r<count)
        {
            for (int i = 0; i < count; i++)
            {
                if(numArr[i] == num)
                {
                    _index = i;
                    break;
                }                    
                
            }
            count--;
            arr = numArr;
            for (int i = 0; i < count; i++)
            {
                if (i >= _index)
                {
                    numArr[i] = arr[i+1];
                }
                else
                {
                    numArr[i] = arr[i];
                }                   
            }
        }
    }
    int arrLen;
    int T;       
    //添加数组的方法
    public void AddRange(int[] Arr)
    {

        if (count > Arr.Length)
        {
            count += Arr.Length;
            arrLen = count - Arr.Length ;
            T = 2;
        }
        else if (count == Arr.Length&&Arr.Length!=0)
        {
            count += Arr.Length;
            arrLen = Arr.Length;
            T = 1;
        }
        else if (count < Arr.Length)
        {
            count += Arr.Length;
            arrLen = count - Arr.Length;
            T = 0;
        }       
        if (Capcity1 == 0)
        {
            Capcity1 = Arr.Length;
            arr = new int[Capcity1];

            for (int i = 0; i < numArr.Length; i++)
            {
                arr[i] = numArr[i];
            }
            numArr = new int[Capcity1];
            for (int i = 0; i < Capcity1; i++)
            {
                numArr[i] = arr[i];
            }
        }
        else if (count>= Capcity1&&Capcity1!=0)
        {
            Capcity1 = (Arr.Length + Capcity1) * 4;
            arr = new int[Capcity1];
            for (int i = 0; i < numArr.Length; i++)
            {
                arr[i] = numArr[i];
            }
            numArr = new int[Capcity1];
            for (int i = 0; i < Capcity1 / 4; i++)
            {
                numArr[i] = arr[i];
            }
        }
        if (T == 2)
        {
            for (int i = count - Arr.Length ; i < count; i++)//4 
            {
                numArr[i] = Arr[i - arrLen];//0.1.2.3.4
            }
        }
        else if (T == 1)
        {
            for (int i = Arr.Length; i < count; i++)
            {
                numArr[i] = Arr[i - arrLen];
            }
        }
        else if (T == 0)
        {
            for (int i = arrLen; i < count; i++)
            {
                numArr[i] = Arr[i - arrLen];
            }
        }

    }
    //删除一定范围内的集合元素
    public void RemoveRange(int index,int max)
    {
        if (index >= 0 && max <= count && max > index)
        {
            count = count - max + index;
            arr = numArr;
            for (int i = 0; i < count; i++)
            {
                if (i >= index )
                {
                    numArr[i] = arr[i - index + max];
                }
                else
                {
                    numArr[i] = arr[i];
                }                    
            }
        }
        else
        {
            Console.WriteLine("你添加的索引有问题!");
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值