【Unity3D数据集合】(一)数组集合Array学习

推荐阅读

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

一、前言

在日常开发中,常常会用到数据集合,那么数据集合是什么呢,数据集合也没有想象中那么复杂。

数据集合就是专门用来存储数据、检索数据,以及对数据一系列操作的类。

这些类有:ArrayList数组、List列表、Queue队列、Dictionary字典、Hashtable哈希表、Stack堆栈。

在开发中,每种数据集合都有优缺点,今天就将这些数据集合进行归纳总结。

一是方便自己捋顺思路,二是可以帮助到对此理解不清晰的开发者。

这是本系列文章的第一篇:
【Unity3D数据集合】(一)数组集合Array学习
【Unity3D数据集合】(二)列表集合List及ListArray学习
【Unity3D数据集合】(三)字典Dictionary和哈希表Hashtable学习
【Unity3D数据集合】(四)堆栈Stack和队列Queue学习
【Unity3D数据集合】(五)链表LinkedList数据集合学习
【Unity3D数据集合】(六)散列集合HashSet和排序集合SortedSet学习
【Unity3D数据集合】(七)排序列表SortedList和排序字典SortedDictionary学习
【Unity3D数据集合】(八)点阵列BitArray学习

二、数组集合介绍

数组是一种数据结构,可以包含同一个类型的多个元素数据。

一个变量只能存放一个值,如果要计算10个变量的最大值,就很麻烦,需要定义10个变量。

所以,就引入了数组的概念,可以存放多个相同类型的值,然后根据数据在数组中的下标位置,就可以读取到数组中的想要找到的数据的值。

同一个数组中存放的值是同一数据类型,可以通过循环或者数据操作的方式对值进行运算或操作。

所有的数组都是由连续的内存位置存放的,最低的地址对应第一个元素,最高的地址对应最后一个元素。

C#中数组从零开始索引,即数组索引从零开始。

三、数组数据初始化

声明数组

在初始化数组之前,我们先说一下声明数组。

在C#中声明数组,需要在类型后面加上方括号[],后面是一个变量名,也就是这样:

int[] table;

放在类型前面,或者标识符后面都是不合法的语法。

下面就介绍如何初始化数组。

数组初始化

声明了数组后,需要初始化数组,然后为数组分配内存,以此才能对数据进行操作。

数组是引用类型,在堆上分配内存,所以需要使用new关键词,后面加上数组的类型和数量来初始化数组的变量。

初始化数组,需要提前设置数组的大小,设置完大小后,数组的长度不能再修改。

初始化数组,通常有4个形式:

1、只声明数组和初始化数组,数组里面的值都为空Null(数字类型的数组默认值为0)

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    void Start()
    {
        table = new int[4];

        for (int i = 0; i < table.Length; i++)
        {
            Debug.Log(table[i]);
        }
    }
}

2、 使用数组初始化器为每个数组元素赋值

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    void Start()
    {
        table = new int[4] { 1, 2, 3, 4 };

        for (int i = 0; i < table.Length; i++)
        {
            Debug.Log(table[i]);
        }
    }
}

3、使用花括号初始化数组,可以不指定数组的大小,因为编译器会自动计算元素的个数:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    void Start()
    {
        table = new int[] { 1, 2, 3, 4 };

        for (int i = 0; i < table.Length; i++)
        {
            Debug.Log(table[i]);
        }
    }
}

4、省略new关键字,直接进行元素赋值:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        int[] table = { 1, 3, 5, 7 };

        for (int i = 0; i < table.Length; i++)
        {
            Debug.Log(table[i]);
        }
    }
}

二维数组初始化

当然,这是一维数组,那么二维数组怎么初始化呢:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[,] table;
    void Start()
    {
        table = new int[5, 2] { { 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 } };

        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                Debug.Log(table[i, j]);
            }
        }
    }
}

同理,也可以不指定二维数组的个数:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[,] table;
    void Start()
    {
        table = new int[, ] { { 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 } };

        for (int i = 0; i < table.GetLength(0); i++)
        {
            for (int j = 0; j < table.GetLength(1); j++)
            {
                Debug.Log(table[i, j]);
            }
        }
    }
}

四、数组数据的增删改查

查找数据

访问数据,可以使用下标进行访问:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    void Start()
    {
        table = new int[] { 1, 2, 3, 4 };

        Debug.Log(table[2]);
    }
}

查找数据,就需要在循环中进行查找了,比如找到数组所有大于1的元素:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    void Start()
    {
        table = new int[] { 1, 2, 3, 4 };

        for (int i = 0; i < table.Length; i++)
        {
            if (table[i] > 1)
            {
                Debug.Log(table[i]);
            }
        }
    }
}

修改数据

可以直接通过下标进行数据修改:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    void Start()
    {
        table = new int[] { 1, 2, 3, 4 };
        table[2] = 10;
    }
}

也可以在循环中,找到对应的数据进行修改:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    void Start()
    {
        table = new int[] { 1, 2, 3, 4 };
        for (int i = 0; i < table.Length; i++)
        {
            if (table[i] > 1)//找到数组中数据大于1的元素
            {
                table[i] += 1;//让数据元素加1
                Debug.Log(table[i]);
            }
        }
    }
}

增加数据

因为数组的长度是固定的,所以不能增加元素

删除数据

因为数组的长度是固定的,所以不能删除元素

五、数组集合的常见函数

拷贝数组

将table里面的元素数据,拷贝给table2:

using UnityEngine;

public class Demo5 : MonoBehaviour
{
    int[] table;
    int[] table2;
    void Start()
    {
        table = new int[] { 1, 2, 3, 4 };

        table2 = new int[table.Length];
        table.CopyTo(table2, 0);

        for (int i = 0; i < table.Length; i++)
        {
            Debug.Log(table2[i]);
        }
    }
}

六、数组集合的优缺点

优点

可以索引坐标访问 读取快

缺点

增删慢,长度不变

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

恬静的小魔龙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值