zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【Unity3D数据集合】(七)排序列表SortedList和排序字典SortedDictionary学习

2023-09-11 14:19:57 时间

推荐阅读

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新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学习

二、排序列表SortedList

排序列表SortedList是一种特殊的数据结构,用来表示键值对的集合,这些键值对都可以通过键和索引来进行访问。

SortedList中的键值总是按照键值来进行排序,SortedList是Hashtable和Array的结合,当通过元素的键访问元素时,比较像哈希表Hashtable;当使用GetByIndex元素索引访问元素时,比较像Array。

所以顺序基于排序顺序,SortedList在操作上要比Hashtable操作要慢,但是SortedList允许通过相关键和索引对值进行访问,更加的灵活。

下面就来看一下排序列表SortedList的使用:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        SortedList table = new SortedList();
        table.Add("5", "50");
        table.Add("1", "10");
        table.Add("4", "40");
        table.Add("3", "30");

        //获取键的集合
        foreach (string item in table.Keys)
        {
            Debug.Log(table[item]);
        }
    }
}

三、排序字典SortedDictionary

排序字典SortedDictionary,是一个泛型类,检索运算复杂度为O(log n)的二叉搜索树,其中的n是字典中的元素数。

SortedDictionary也是一个键值对类型的数据结构,需要比较器实现来执行键比较。可以使用一个接受 comparer 参数的构造函数来指定 IComparer 泛型接口的实现;如果不指定实现,则使用默认的泛型比较器 Comparer.Default。如果类型 TKey 实现 System.IComparable 泛型接口,则默认比较器使用该实现。

C# 语言的 foreach 语句,需要集合中每个元素的类型。由于 SortedDictionary 的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 KeyValuePair 类型。

下面的代码演示SortedDictionary的用法:

3-1、创建及初始化

SortedDictionary<T, T> table = new SortedDictionary<T, T>();

T是泛型类型,可以是string、int,也可以是自定义类型

3-2、添加元素

通过Add()函数来添加元素

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        SortedDictionary<string, string> table = new SortedDictionary<string, string>();
        table.Add("5", "50");
    }
}

3-3、通过Key查找元素

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        SortedDictionary<string, string> table = new SortedDictionary<string, string>();
        table.Add("5", "50");
        table.Add("1", "10");
        table.Add("4", "40");
        table.Add("3", "30");

        if (table.ContainsKey("5"))
        {
            Debug.Log(table["5"]);
        }
    }
}

3-4、通过KeyValuePair遍历元素

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        SortedDictionary<string, string> table = new SortedDictionary<string, string>();
        table.Add("5", "50");
        table.Add("1", "10");
        table.Add("4", "40");
        table.Add("3", "30");

        foreach (KeyValuePair<string, string> item in table)
        {
            Debug.Log(item.Key + " " + item.Value);
        }
    }
}

3-5、遍历键Key或遍历值Value

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        SortedDictionary<string, string> table = new SortedDictionary<string, string>();
        table.Add("5", "50");
        table.Add("1", "10");
        table.Add("4", "40");
        table.Add("3", "30");

        //遍历键
        foreach (string Keys in table.Values)
        {
            Debug.Log(Keys);
        }

        //遍历值
        foreach (string Value in table.Keys)
        {
            Debug.Log(Value);
        }
    }
}

3-6、移除指定的键值

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Demo5 : MonoBehaviour
{
    void Start()
    {
        SortedDictionary<string, string> table = new SortedDictionary<string, string>();
        table.Add("5", "50");
        table.Add("1", "10");
        table.Add("4", "40");
        table.Add("3", "30");

        if (table.ContainsKey("1"))
        {
            table.Remove("1");
        }
    }
}

3-7、 其他常见属性及方法

属性及方法介绍
Comparer:获取用于确定字典中的键是否相等的 IEqualityComparer。
Count:获取包含在 Dictionary中的键/值对的数目。
Item:获取或设置与指定的键相关联的值。
Keys:获取包含 Dictionary中的键的集合。
Values:获取包含 Dictionary中的值的集合。
Add:将指定的键和值添加到字典中。
Clear:从 Dictionary中移除所有的键和值。
ContainsKey:确定 Dictionary是否包含指定的键。
ContainsValue:确定 Dictionary是否包含特定值。
GetEnumerator:返回循环访问 Dictionary的枚举数。
GetType:获取当前实例的 Type。 (从 Object 继承。)
Remove:从 Dictionary中移除所指定的键的值。
ToString:返回表示当前 Object的 String。 (从 Object 继承。)
TryGetValue:获取与指定的键相关联的值。