制作一个属于自己的列表

要求:定义添加的方法   定义检测数据是否存在的方法  定义一个排序的方法 定义插入数据的方法  定义属性 查看数据的长度  定义属性查看数据的个数  定义一个索引器访问设置数据 

internal class MyList<T>
    {
        public T[] data = new T[0];
        // count 用来代表数组的个数
        public int count = 0;

        // 定义一个属性,访问这个属性的时候,可以获取数组的长度,容量
        public int Capacity
        {
            get { return data.Length; }
        }
        // 定义一个属性,访问这个属性的时候,可以获取数据的个数
        public int Count
        {
            get { return count; }
        }
        // 定义一个添加数据的方法
        public void Add(T item)
        {
            // 添加前,先判断数组长度是不是0,如果是0就给数组重新赋值,将数组长度改为4
            if (data.Length == 0)
            {
                data = new T[4];
            }
            if (data.Length == count)
            {
                T[] temp = new T[count * 2];
                for(int i = 0; i < data.Length; i++)
                {
                    temp[i] = data[i];
                }
                data = temp;
            }
            data[count] = item;
            count++;
        }
        // 定义一个索引器,通过索引器访问数组中某个数据
        public T this[int index]
        {
            get 
            { 
                if (index < 0 || index > Count - 1)
                {
                    // throw 抛出异常
                    throw new IndexOutOfRangeException("超出索引长度");
                }
                return data[index];
            }
            set 
            { 
                data[index] = value;
            }
        }
        // 定义一个方法,用来在某个位置插入数据
        public void Insert(int index , T item)
        {
            if (index < 0 || index > Count - 1)
            {
                // throw 抛出异常
                throw new IndexOutOfRangeException("超出索引长度");
            }
            for (int i = Count-1; i > index-1; i--)
            {
                data[i + 1] = data[i];
            }
            data[index] = item;
        }
       // 定义一个方法,用来检测某个数据是否存在
       public int Indexof(T item)
        {
            int index = -1;
            for (int i = 0;i < Count;i++)
            {
                if (data[i].Equals(item))
                {
                    index = i; 
                    break;
                }
            }
            return index;
        }
        public int LastIndexof(T item)
        {
            int index = -1;
            for (int i = Count-1; i >= 0; i--)
            {
                if (data[i].Equals(item))
                {
                    index = i;
                    break;
                }
            }
            return index;
        }
        // 排序
        //public void Sort()
        //{
        //    Array.Sort(data, 0, Count);
        //}
        public void Sort()
        {
            for (int i = 0; i < Count-1; i++)
            {
                for (int j = 0; j < count-1-i;j++)
                {
                    dynamic num = data[j];
                    dynamic num1 = data[j+1];
                    if (num>num1)
                    {
                        dynamic temp = data[j];
                        data[j] = data[j+1];
                        data[j+1] = temp;
                    }
                }
            }
        }

    }
internal class Program
    {
        static void Main(string[] args)
        {
            // 制作一个属于自己的列表  定义添加的方法   定义检测数据是否存在的方法  定义一个排序的方法 定义插入数据的方法  定义属性 查看数据的长度  定义属性查看数据的个数  定义一个索引器访问设置数据
            MyList<int> list = new MyList<int>();
            // 调用MyList里面的Capacity属性,获取数组的长度,容量
            Console.WriteLine("数组的容量: " + list.Capacity);
            list.Add(1);
            list.Add(12);
            list.Add(22);
            list.Add(88);
            list.Add(5);
            list.Add(18);
            list.Add(36);
            list.Add(88);
            list.Add(10);
            list.Add(6);
            // 调用MyList里面的Count属性,获取数组长度
            Console.WriteLine("数组长度: " + list.Count);
            // 调用MyList中的索引器,更改值
            list[2] = 99;
            // 调用MyList中的索引器,访问值
            Console.WriteLine("访问更改后的值: " + list[2]);
            // 调用MyList里面的Insert方法,在某个位置插入数据
            list.Insert(5, 1000);
            for (int i = 0; i < list.Count; i++)
            {
                Console.Write(list[i]+ " ");
            }
            Console.WriteLine();
            // 调用MyList中的Indexof,检测某一个数据是否存在;从前往后,第一次出现的
            Console.WriteLine(list.Indexof(88));
            // 调用MyList中的LastIndexof(,检测某一个数据是否存在;从后往前,第一次出现的
            Console.WriteLine(list.LastIndexof(88));
            // 调用MyList中的Sort,排序
            list.Sort();
            for (int i = 0;i < list.Count;i++)
            {
                Console.Write(list[i] + " ");
            }
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值