Array and List Task_v4

Revised Task


namespace ArrayList
{

    public class MyList
    {
        public int[] array;
        public int size = 0;
        public MyList(int size)
        {
            array = new int[size];
        }
        public MyList()
        {
            array = new int[0];
        }
        //数组internal array
        public int Size
        {
            get { return size; }
        }
        //数组长度
        public int Length
        {
            get { return array.Length; }
        }
        //添加元素至数组最后
        public void Add(int element)
        {
            if (size <= array.Length) //判断已“添加”的元素数量是否超过长度
            {
                array[size] = element;
                size++;
            }
            else
            {
                throw new NotImplementedException("Out of index");
            }

        }
        //查找某个位置元素
        public int ElementAt(int index)
        {
            if (index >= 0 & index < size) //判断查找的位置是否超过长度
            {
                return array[index];
            }
            else
            {
                throw new NotImplementedException("Out of index");
                // 索引超出范围,返回提示消息
            }
        }
        //移除某位置元素
        public void RemoveAt(int index)
        {
            if (size == array.Length)
            {
                array[index] = 0;
                
            }
            else
            {
                if (index >= 0 & index < size) //判断是否在超出索引范围
                {
                    for (int i = index + 1; i <= size; i++) //选择的位置开始选取后面1位的值
                    {
                        array[i - 1] = array[i];

                    }
                    size--;
                }
                else
                {
                    throw new NotImplementedException("not enough index");
                }
            }

        }
        //在某位置插入一个元素

        public void InsertAt(int element, int index)
        {
            if (index < array.Length) //判断是否在超出索引范围 external list
            {
                if (index >= 0 & index <= size) //判断是否在internal array里
                {
                    for (int i = size - 1; i >= index; i--)
                    {
                        array[i + 1] = array[i]; //后移插入位置后的元素
                    }
                    array[index] = element; //插入位置的元素
                    size++;
                    /*
                                        for (int i = size - 1; i >= 0; i--)
                                        {
                                            if (i > index)
                                                NewArray[i + 1] = array[i]; //把insert位置和其后的值向后移动一个单位
                                            else if (i < index)
                                                NewArray[i] = array[i]; //insert位置前新旧array相等
                                            else
                                                NewArray[i] = element; //insert位置 为新插入元素
                                        }
                                        array = NewArray;
                                        size++;*/
                }
                else
                {
                    array[size] = element;
                    size++; //若在external list和internal array之间,直接添加

                }
            }
            else
            {
                throw new NotImplementedException("not enough index");
            }
        }
        public void display() //display the list
        {
            Console.WriteLine("your list now contains:");
            Console.WriteLine(" ");
            for (int j = 0; j < array.Length; j++)
            {
                Console.WriteLine(array[j]);
            }
            Console.ReadLine();
        }
    }
    static class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Input the length of your list:");
            int listLength = int.Parse(Console.ReadLine());
            MyList array = new MyList(listLength);
            Console.WriteLine(" ");
            Console.Write("how many numbers you want to add now?");
            int num = int.Parse(Console.ReadLine());
            Console.WriteLine(" ");
            if (num > listLength)
            {
                Console.Write("Numbers cannot exceed your length, reinput numbers:");
                num = int.Parse(Console.ReadLine());
            }  
            //添加3个元素
            for (int k = 0; k < num; k++)
            {
                Console.Write("Enter a number you want to add:");
                int newElement = int.Parse(Console.ReadLine());
                array.Add(newElement); //添加元素到数组
            }
            array.display();
            Console.WriteLine("internal array length is:" + array.Size);
            Console.WriteLine("external list length is:" + array.Length);
            Console.Write("Enter the index you want to find:");
            int indexElement = int.Parse(Console.ReadLine());
            int i = array.ElementAt(indexElement);//找某个位置的元素
            Console.Write("The element you located is:" + i);
            Console.WriteLine(" ");
            //移除指定位置的元素
            Console.Write("Enter the index of a number you want to remove:");
            int removeIndex = int.Parse(Console.ReadLine());
            array.RemoveAt(removeIndex);
            array.display();
            //添加元素到具体位置
            Console.Write("Enter a number you want to insert:");
            int _newelement = int.Parse(Console.ReadLine());
            Console.Write("Enter the index of where you want to insert it:");
            int newindex = int.Parse(Console.ReadLine());
            array.InsertAt(_newelement, newindex);
            array.display();
            

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值