【数据结构】二次封装自己的数组(二)升级为泛型数组

2024-04-23 19:03:15 浏览数 (1)

升级后的泛型数组

代码语言:javascript复制
 public class Array<E>
    {
        private E[] data;
        private int size;
        //构造函数,传入数组的容量capacity构造Array
        public Array(int capacity)
        {
            data = new E[capacity];
            size = 0;
        }
        //无参数构造函数,传入数组的容量capacity=10
        public Array() : this(10)
        {

        }
        //获取数组元素个数
        public int getSize()
        {
            return size;
        }
        //获取数组的容量
        public int getCapacity()
        {
            return data.Length;
        }
        //返回数组是否为空
        public bool isEmpty()
        {
            return size == 0;
        }
        //向所有元素之后添加一个新元素
        public void addLast(E e)
        {
            add(size, e);
        }
        public void addFirst(E e)
        {
            add(0, e);
        }
        //在index位置插入一个新元素e
        public void add(int index, E e)
        {
            if (size == data.Length)
                throw new ArgumentException("Add failed.Array is full");
            if (index < 0 || index > size)
                throw new ArgumentException("Add failed.Require index < 0 || index > size");
            for (int i = size - 1; i >= index; i--)
                data[i   1] = data[i];
            data[index] = e;
            size  ;
        }
        //获取index索引位置的元素
        E get(int index)
        {
            if (index < 0 || index >= size)
                throw new ArgumentException("Get failed.Index is illegal");
            return data[index];
        }
        //修改index索引位置的元素e
        void set(int index, E e)
        {
            if (index < 0 || index >= size)
                throw new ArgumentException("Set failed.Index is illegal");
            data[index] = e;
        }
        //查找数组中是否有元素e
        public bool contains(E e)
        {
            for (int i = 0; i < size; i  )
            {
                if (data[i].Equals(e))
                    return true;
            }
            return false;
        }
        //查找数组中元素e所在的索引,如果不存在元素e,则返回-1
        public int find(E e)
        {
            for (int i = 0; i < size; i  )
            {
                if (data[i].Equals(e))
                    return i;
            }
            return -1;
        }
        //删除指定索引位置的元素
        public E remove(int index)
        {
            if (index < 0 || index >= size)
                throw new ArgumentException("Get failed.Index is illegal");
            E ret = data[index];
            for (int i = index   1; i < size; i  )
            {
                data[i - 1] = data[i];
            }
            size--;
            data[size] = default(E);
            return ret;
        }
        //从数组中删除第一个元素,返回删除的元素
        public E removeFirst()
        {
            return remove(0);
        }
        //从数组中删除最后一个元素,返回删除的元素
        public E removeLast()
        {
            return remove(size - 1);
        }
        //从数组中删除元素e(只删除一个e)
        public void removeElement(E e)
        {
            int index = find(e);
            if (index != -1)
                remove(index);
        }
        //重写ToString
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"Array:size = {size}, capacity = {getCapacity()}n");
            sb.Append("[");
            for (int i = 0; i < size; i  )
            {
                sb.Append(data[i]);
                if (i != size - 1)
                    sb.Append(",");
            }
            sb.Append("]");
            return sb.ToString();
        }
    }

下面我们用一个类来测试一下,创建一个Student类

代码语言:javascript复制
class Student
    {
        private string name;
        private int score;
        public Student(string studentName,int studentScore)
        {
            name = studentName;
            score = studentScore;
        }

        public override string ToString()
        {
            return String.Format("Student(name: {0:s},score: {1:d})",name,score);
        }
    }

主方法运行

代码语言:javascript复制
 class Program
    {
        static void Main(string[] args)
        {
            Array<Student> arr = new Array<DataStructure.Student>();
            arr.addLast(new Student("Alice", 100));
            arr.addLast(new Student("Bob", 66));
            arr.addLast(new Student("Charlie", 80));
            Console.WriteLine(arr);
            Console.ReadKey();
        }
    }

结果,运行成功

在这里插入图片描述在这里插入图片描述

0 人点赞