SortedDictionary和SortedList

使用上两者的接口都类似字典,并且SortedList的比如Find,FindIndex,RemoveAll常用方法都没提供。

数据结构上二者差异比较大,SortedList查找数据极快,但添加新元素,删除元素较慢,SortedDictionary查找,添加,删除速度都比较平均。

博友的测试结果:

直接在Unity3D里测一下

public class Test : MonoBehaviour
{
    void Start()
    {
        var sortedDict = new SortedDictionary<string, int>();
        sortedDict.Add("a01", 10);
        sortedDict.Add("a10", 2);
        sortedDict.Add("a03", 5);
        sortedDict.Add("a02", 1);

        print(sortedDict["a01"]);

        foreach (var item in sortedDict)
        {
            Debug.Log("SortedDictionary: " + item);
        }

        var sortedList = new SortedList<string, int>();
        sortedList.Add("a01", 10);
        sortedList.Add("a10", 2);
        sortedList.Add("a03", 5);
        sortedList.Add("a02", 1);

        print(sortedList["a01"]);

        foreach (var item in sortedList)
        {
            Debug.Log("SortedList: " + item);
        }
    }
}
View Code

调用结果:

原文地址:https://www.cnblogs.com/hont/p/5420060.html