C# 使用泛型和索引器来实现一个集合类列表MyList

有下面的方法和属性

1、Capacity获取容量大小

2、Add()方法添加元素

3、Insert()方法插入元素

4、[index]访问元素(索引器)

5、Count属性访问元素个数

6、RemoveAt()方法移除指定位置的元素

7、IndexOf()方法取得一个元素所在列表中的索引位置

      LastIndexOf()上面的方法是从前往后搜索,这个是从后往前搜索,搜索到满足条件的就停止

      上面的两个方法,如果没有找到指定元素就返回-1

8、Sort()对列表中是元素进行从小到大排序

索引器:通过[index]这种形式去访问数据,就是索引器

MyList

class MyList<T> where T: IComparable //where约束 表示T必须是继承IComparable的
{
    private T[] array;
    private int count=0;//表示当前添加的元素的个数
    //构造函数
    public MyList(int size)
    {
        if (size >= 0)
        {
            array = new T[size];
        }
    }

    public MyList()
    {
        array = new T[0];
    }

    //1、Capacity获取容量大小
    public int Capacity
    {
        get { return array.Length; }
    }

    //5、Count属性访问元素个数
    public int Count
    {
        get { return count; }
    }

    //2、Add()方法添加元素
    public void Add(T item )
    {
        if (Count == Capacity) //判断元素个数跟列表容量大小是否一样大,如果一样大,说明数组容量不用,需要创建新的数组
        {
            if (Capacity == 0)
            {
                array = new T[4];//当数组长度为0的时候,创建一个长度为4的数组
            }
            else
            {
                var newArray = new T[Capacity*2];//当长度不为0的时候,我们创建一个长度为原来2倍的数组
                Array.Copy(array,newArray,Count);//把旧数组中的元素复制到新的数组中 , 复制前count个  array-->newArray
                array = newArray;
            }
        }
        array[Count] = item;
        count++;//元素个数自增
    }
    //通过索引返回数据
    public T GetItem(int index)
    {
        if (index >= 0 && index <= count - 1)
        {
            return array[index];
        }
        else
        {
            //Console.WriteLine("所以超出了范围");
            throw new Exception("索引超出了范围");//抛出异常
        }
    }

    //添加索引器,通过索引器访问元素
    //4,[index]访问元素(索引器)
    public T this[int index]
    {
        get//当我们通过索引器取值的时候,会调用get块
        { return GetItem(index); }
        set//当我们通过索引器设置值的时候,会调用set块
        {
            if (index >= 0 && index <= count - 1)
            {
                array[index] = value;
            } else {
                //Console.WriteLine("所以超出了范围");
                throw new Exception("索引超出了范围");
            }
        }
    }
    //3、Insert()方法插入元素
    public void Insert(int index, T item)
    {
        if (index >= 0 && index <= count - 1)
        {
            if (Count == Capacity)//容量不够 进行扩容
            {
                var newArray = new T[Capacity*2];   
                Array.Copy(array,newArray,count);//复制数组
                array = newArray;
            }
            //从最后一个元素往前遍历,把前面的元素放到后面
            for (int i = count-1; i >=index; i--)
            {
                array[i + 1] = array[i];//把i位置的值放在后面,就是向后移动一个单位

            }
            array[index] = item;
            count++;//元素个数自增
        }
        else
        {
            throw new Exception("索引超出范围");
        }
    }
    //6、RemoveAt()方法移除指定位置的元素
    public void RemoveAt(int index)
    {
        if (index >= 0 && index <= count - 1)
        {
            //将要移除元素的后面所有的元素向前移动一个位置
            for (int i = index + 1; i < count; i++)
            {
                array[i - 1] = array[i];//把值赋给前面的元素
            }
            count--;//元素个数自减
        }
        else
        {
            throw new Exception("所以超出范围");
        }
    }
    //7、IndexOf()方法取得一个元素所在列表中的索引位置
    public int IndexOf(T item)
    {
        for (int i = 0; i < count; i++)
        {
            if (array[i].Equals(item))//返回指定的对象是否等于当前对象。
            {
                return i;
            }
        }
        return -1;
    }
    //7、LastIndexOf()上面的方法是从前往后搜索,这个是从后往前搜索,搜索到满足条件的就停止
    public int LastIndexOf(T item)
    {
        //从后往前遍历
        for (int i = Count-1; i >=0; i--) {
            if (array[i].Equals(item)) {
                return i;
            }
        }
        return -1;
    }
    //8、Sort()对列表中是元素进行从小到大排序
    public void Sort()
    {
        for (int j = 0; j < Count-1; j++)//执行count-1次for循环
        {
            for (int i = 0; i < Count - 1 - j; i++) {//一个for循环排序
                //CompareTo 小于0排在参数前面,大于0排在参数后面
                if (array[i].CompareTo(array[i + 1]) > 0) {
                    T temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
        }
           
    }
}

调用

class Program {
    static void Main(string[] args) {
            
        MyList<int> mylist = new MyList<int>();
        mylist.Add(234);//添加元素
        mylist.Add(14);
        mylist.Add(24);
        mylist.Add(24);
        mylist.Add(90);
        mylist.Add(274);
        //mylist[index]
        mylist.Insert(1,2);//插入元素
        mylist.RemoveAt(5);//移除元素
        mylist[0] = 100;//通过索引器 设置值
        for (int i = 0; i < mylist.Count; i++)
        {
            Console.WriteLine(mylist.GetItem(i));
            Console.WriteLine(mylist[i]);//通过索引器 取值
        }
        Console.WriteLine(mylist.IndexOf(24));
        Console.WriteLine(mylist.LastIndexOf(24));
        mylist.Sort();//排序
        Console.WriteLine();
        for (int i = 0; i < mylist.Count; i++) {
            //Console.WriteLine(mylist.GetItem(i));
            Console.WriteLine(mylist[i]);//通过索引器 取值
        }
        Console.ReadKey();
    }
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值