【Unity3D数据集合】(七)排序列表SortedList和排序字典SortedDictionary学习
推荐阅读
大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新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: | 获取与指定的键相关联的值。 |
相关文章
- 并发集合(五)使用线程安全的、带有延迟元素的列表
- Qt-QMenu的自定义内容-在右击列表中增加一个EDIT框
- 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建
- python中列表(list)函数及使用
- 注意!在subList生成子列表之后,一定不要随便更改原列表
- 好友聊天消息列表本地数据库封装
- Jquery实现Bootstrap树形列表
- python生成式:列表、字典、集合
- Python 随机列表、集合、元组、字典的生成和排序
- SAP Spartacus B2B user列表对应的Angular Component
- Atitit vue绑定json列表数据到表格最小简化培训版本 目录 1.1. 引入类库jq与vue1 1.2. 获取ajax数据并推送到vue的内部自定义变量1 1.3. 建立表格与vue变量
- Atitit 数据结构与常见文件元数据结构 目录 1. 分类 内部数据结构与外部存储数据结构1 2. 编程语言内部数据结构 (堆栈 树 图等1 2.1. 数据结构 (集合,列表,tree,map
- Atitit data struts art 数据结构的艺术 数据结构之道 attilax著 目录 1. 分类1 1.1. 按照元素关系分(集合,列表,tree,map,图)1 1.2. 按
- Python语言学习:Python语言学习之容器(列表&元组&字典&集合)简介、特点/意义/经验总结及容器魔法方法(定义可变&不可变容器的协议)的简介、案例应用之详细攻略
- 〖Python零基础入门篇㉒〗- Python列表常用方法
- Python编程:列表、集合、字典推导式的示例
- img、列表和table标签
- 匿名函数(lambda)在列表生成式和生成器中的应用示例
- 002-Python3-基础语法-赋值、显示类型、数据类型[数值、字符串、列表、元祖、集合、字典]
- Emacs显示函数列表imenu-list(一)
- GetLastError()返回值列表
- Hibernate一对多映射列表实例(使用xml文件)
- 集合映射中的映射列表(使用xml文件)
- Dart基础第5篇:自增自减运算符、for、while、do...while循环、continue、break、多维列表循环