数据结构学习(二)数组

数据结构学习(二)数组

本文使用C# 编码,简单的演示了自定义Int类型和泛型类型的数组。以此来学习数组这种数据结构,了解数组数据的存储,以及如何实现一个简单的数组。

1. 数组基础

内存中连续的一段内存空间
有序的
可以通过索引下标很快的定位到元素
新增和删除时候可能会有元素的位置移动

2. 自定义数组

各种编程语言已经给我吗封装好了可使用的数组,而且功能齐全,那为什么我们还要在这里再去照虎画猫呢,是多此一举吗?如果想了解数组底层的实现原理,不妨自己试试封装一个简单的数组试试。会用也得知其原理,才能用的更好。

先自定义一个只能存储int类型数据的数组,了解一下数组的数据存储结构以。再自定义一个可以存储objc类型的数组,更深一步了解一下简单数组的实现原理与思路。

2.1 自定义Int类型的数组

自定义的Int类型的数组,初始化的时候就规定死了,数组中只能存储Int类型的数据,这有很大的局限性,通过自定义泛型来自定义数组能解决这个问题。

using System;

namespace ConsoleAppDemo
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            MMIntArray intArray = new MMIntArray(20);
            intArray.AddtFirst(1);
            intArray.AddAtLast(20);
            intArray.AddAtIndex(0, 0);
            intArray.AddAtIndex(0, 4);

            int i = intArray.GetAtIndex(0);
            int j = intArray.GetAtIndex(1);
            int k = intArray.GetAtIndex(2);
            Console.WriteLine(i.ToString());
            Console.WriteLine(j.ToString());
            Console.WriteLine(k.ToString());
            
        }
    }

    // 自定义Int类型的数组
    class MMIntArray
    {
        private int[] data; // 整形数组
        private int size;   // 数组当前的元素个数/容量

        // 初始化方法
        public MMIntArray(int capacity)    // 传入参数capacity,设置数组的总容量
        {
            data = new int[capacity];
            size = 0;   // 初始是个空数组,当前无元素,size为0。
        }

        // 初始化方法
        public MMIntArray()
        {
            //MMarray(10); ?
        }

        // 获取数组容量
        public int GetCapacity()
        {
            return data.Length;
        }

        // 获取数组元素个数
        public int GetSize()
        {
            return size;
        }

        // 数组是否为空
        public bool IsEmpty()
        {
            return size == 0;
        }

        // 末尾添加
        public void AddAtLast(int ele)
        {
            AddAtIndex(ele, size);
        }
        // 首位添加
        public void AddtFirst(int ele)
        {
            AddAtIndex(ele, 0);
        }
        // 任意位置添加
        public void AddAtIndex(int ele, int index)
        {
            if (size == data.Length)  // 如果当前元素个数 = 数组总长度,则数组容量已满
            {
                throw new Exception("array is full");
            }
            if (index < 0 || index > size)
            {
                throw new Exception("Invalid");
            }
            for (int i = size - 1; i >= index; i--) // 将index位置以后的元素都向后移一个位置
            {
                data[i + 1] = data[i];
            }
            data[index] = ele;
            size++;
        }

        // 删除首位置
        public void RemoveAtFirst()
        {
            RemoveAtIndex(0);
        }
        // 删除首位置
        public void RemoveAtLast()
        {
            RemoveAtIndex(size - 1);
        }
        // 删除任意位置的元素
        public void RemoveAtIndex(int index)
        {
            if (index < 0 || index >= size)
            {
                return;
            }
            for (int i = index; i < size - 1; i++) // 将index位置以后的元素都向前移一个位置
            {
                data[i] = data[i + 1];
            }
            data[size] = 0;
            size--;
        }
        // 删除某元素
        public void Remove(int ele)
        {
            int index = GetIndex(ele);
            if (index > 0)
            {
                RemoveAtIndex(index);
            }
        }

        // 获取某元素的下标
        public int GetIndex(int ele)
        {
            for (int i = 0; i < size; i++)
            {
                if (ele == data[i])
                {
                    return i;
                }
            }
            return -1;
        }

        // 替换index位置的元素
        public void ReplaceAtIndex(int ele, int index)
        {
            if (index < 0 || index >= size)
            {
                return;
            }
            data[index] = ele;
        }

        // 查下标为index的元素
        public int GetAtIndex(int index)
        {
            if (index < 0 || index >= size)
            {
                throw new Exception("Invalid");
            }
            return data[index];
        }

        // 是否包含
        public bool IsContains(int ele)
        {
            for (int i = 0; i < size; i++) 
            {
                if (ele == data[i])
                {
                    return true;
                }
            }
            return false;
        }
    }
}

2.2 自定义泛型数组

上面自定义的Int类型的数组只能存储Int类型的数据,其他数据就不能存储了,一个数组如果是这样的话那可太坑了。所以需要自定义一个支持某些或者更多类型存储的数组,也可以存储对象类型。

也就是说这样的数组需要存储泛型类型的数据。

using System;

namespace ConsoleAppDemo
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Person p = new Person();
            MMArray<object> array = new MMArray<object>(10);
            array.AddAtLast(11);
            array.AddAtLast(p);
            array.AddtFirst(10);

            object i = array.GetAtIndex(0);
            object j = array.GetAtIndex(1);
            object k = array.GetAtIndex(2);
            Console.WriteLine(i.ToString());
            Console.WriteLine(j.ToString());
            Console.WriteLine(k.ToString());
        }
    }

    // 自定义数组
    class MMArray<T>
    {
        private T[] data;           // 存储素的数组
        private static int size;    // 数组大小

        //public MMArray()
        //{
        //    MMArray(10);
        //}

        // 初始化
        public MMArray(int capacity)    
        {
            data = new T[capacity];
            size = 0;   
        }

        // 获取数组容量
        public int GetCapacity()
        {
            return data.Length;
        }

        // 获取数组大小
        public int GetSize()
        {
            return size;
        }

        // 是否为空
        public bool IsEmpty()
        {
            return size == 0;
        }

        // 添加
        public void AddAtLast(T ele)
        {
            AddAtIndex(ele, size);
        }
        public void AddtFirst(T ele)
        {
            AddAtIndex(ele, 0);
        }
        public void AddAtIndex(T ele, int index)
        {
            if (size == data.Length)  // 如果当前元素个数 = 数组总长度,则数组容量已满
            {
                throw new Exception("array is full");
            }
            if (index < 0 || index > size)
            {
                throw new Exception("Invalid");
            }
            for (int i = size - 1; i >= index; i--) // 将index位置以后的元素都向后移一个位置
            {
                data[i + 1] = data[i];
            }
            data[index] = ele;
            size++;
        }

        // 删除
        public void RemoveAtFirst()
        {
            RemoveAtIndex(0);
        }
        public void RemoveAtLast()
        {
            RemoveAtIndex(size - 1);
        }
        public void RemoveAtIndex(int index)
        {
            if (index < 0 || index >= size)
            {
                return;
            }
            for (int i = index; i < size - 1; i++) // 将index位置以后的元素都向前移一个位置
            {
                data[i] = data[i + 1];
            }
            data[size] = default(T);
            size--;
        }
        public void Remove(T ele)
        {
            int index = GetIndex(ele);
            if (index > 0)
            {
                RemoveAtIndex(index);
            }
        }

        // 获取元素下标
        public int GetIndex(T ele)
        {
            for (int i = 0; i < size; i++)
            {
                if (ele.Equals(data[i]))
                {
                    return i;
                }
            }
            return -1;
        }

        // 替换
        public void ReplaceAtIndex(T ele, int index)
        {
            if (index < 0 || index >= size)
            {
                return;
            }
            data[index] = ele;
        }

        // 获取元素
        public T GetAtIndex(int index)
        {
            if (index < 0 || index >= size)
            {
                throw new Exception("Invalid");
            }
            return data[index];
        }

        // 是否包含
        public bool IsContains(T ele)
        {
            for (int i = 0; i < size; i++)
            {
                if (data[i].Equals(ele))
                {
                    return true;
                }
            }
            return false;
        }
    }


    class Person
    {

    }
}

3. 参考

[1] C# 泛型(Generic): https://www.runoob.com/csharp/csharp-generic.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morris_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值