SortedList 泛型类  
请参见  示例  成员 
 全部折叠 全部展开    语言筛选器: 全部 语言筛选器: 多个 语言筛选器: Visual Basic 语言筛选器: C# 语言筛选器: C++ 语言筛选器: J# 语言筛选器: JScript  
 Visual Basic(声明) 
 Visual Basic(用法) 
 C# 
 C++ 
 J# 
 JScript 
注意:此类在 .NET Framework 2.0 版中是新增的。

表示键/值对的集合,这些键/值对基于关联的 IComparer 实现按照键进行排序。

命名空间:System.Collections.Generic
程序集:System(在 system.dll 中)

语法
Visual Basic(声明) 
<SerializableAttribute> _
<ComVisibleAttribute(False)> _
Public Class SortedList(Of TKey, TValue)
    Implements IDictionary(Of TKey, TValue), ICollection(Of KeyValuePair(Of TKey, TValue)), _
    IEnumerable(Of KeyValuePair(Of TKey, TValue)), IDictionary, ICollection, _
    IEnumerable
 
Visual Basic(用法) 
Dim instance As SortedList(Of TKey, TValue)

C# 
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
public class SortedList<TKey,TValue> : IDictionary<TKey,TValue>, ICollection<KeyValuePair<TKey,TValue>>, 
    IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection, IEnumerable
 
C++ 
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
generic<typename TKey, typename TValue>
public ref class SortedList : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, 
    IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
 
J# 
J# 支持使用泛型类型和方法,但不支持进行新的声明。
 
JScript 
JScript 支持泛型类型和方法。

类型参数
TKey 
集合中键的类型。

TValue 
集合中值的类型。

备注
SortedList 泛型类是具有 O(log n) 检索的二进制搜索树,其中 n 是字典中元素的数目。就这一点而言,它与 SortedDictionary 泛型类相似。这两个类具有相似的对象模型,并且都具有 O(log n) 的检索运算复杂度。这两个类的区别在于内存的使用以及插入和移除元素的速度:

SortedList 使用的内存比 SortedDictionary 少。

SortedDictionary 可对未排序的数据执行更快的插入和移除操作,它的运算复杂度为 O(log n),而 SortedList 的运算复杂度为 O(n)。

如果使用排序数据一次性填充列表,则 SortedList 比 SortedDictionary 快。

SortedDictionary 类和 SortedList 类之间的另一个区别是:SortedList 支持通过由 Keys 和 Values 属性返回的集合对键和值执行高效的索引检索。访问此属性时无需重新生成列表,因为列表只是键和值的内部数组的包装。下面的代码演示如何使用 Values 属性从已排序的字符串列表中按索引检索值:

Visual Basic  复制代码 
Dim v As String = mySortedList.Values(3)
 
C#  复制代码 
string v = mySortedList.Values[3];
 
C++  复制代码 
String^ v = mySortedList->Values[3];

SortedList 作为键/值对的数组来实现,它按键排序。每个元素都可以作为一个 KeyValuePair 对象进行检索。

只要键对象用作 SortedList 中的键,它们就必须是永远不变的。SortedList 中的每个键必须是唯一的。键不能为 空引用(在 Visual Basic 中为 Nothing),但如果列表中值的类型 TValue 为引用类型,则值可以。

SortedList 需要比较器实现来排序和执行比较。默认比较器 Comparer.Default 检查键类型 TKey 是否实现 System.IComparable 以及是否使用该实现(如果可用)。否则,Comparer.Default 将检查键类型 TKey 是否实现 System.IComparable。如果键类型 TKey 未实现任一接口,则您可以在构造函数重载中指定一个接受 comparer 参数的 System.Collections.Generic.IComparer 实现。

SortedList 的容量是指 SortedList 可以保存的元素数。在此实现中,SortedList 的默认初始容量为 16;但此默认值可能在 .NET Framework SDK 的未来版本中更改。当向 SortedList 添加元素后,将通过重新分配内部数组,根据需要自动增大容量。可通过调用 TrimExcess 或通过显式设置 Capacity 属性减少容量。减少容量会重新分配内存并复制 SortedList 中的所有元素。

C# 语言中的 foreach 语句(在 C++ 中为 for each,在 Visual Basic 中为 For Each)需要集合中的元素类型。由于 SortedList 的元素是键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 KeyValuePair 类型。例如:

C#  复制代码 
foreach (KeyValuePair<int, string> kvp in mySortedList) {}
 
C++  复制代码 
for each (KeyValuePair<int, String^> kvp in mySortedList) {}
 
Visual Basic  复制代码 
For Each kvp As KeyValuePair(Of Integer, String) In mySortedList
    
Next kvp

foreach 语句是对枚举数的包装,它只允许从集合中读取,不允许写入集合。

示例
下面的代码示例使用字符串键创建一个空的字符串 SortedList,并使用 Add 方法添加一些元素。此示例演示了当尝试添加重复键时,Add 方法会引发 ArgumentException。

此示例使用 Item 属性(C# 中的索引器)检索值,演示了当请求的键不存在时会引发 KeyNotFoundException,以及与键关联的值可以被替换。

此示例演示如果程序必须经常尝试排序列表中不存在的键值,如何将 TryGetValue 方法作为更有效的值检索方法,以及在调用 Add 方法前,如何使用 ContainsKey 方法测试键是否存在。

此示例演示如何在排序列表中枚举键和值,以及如何使用 Keys 属性和 Values 属性分别枚举键和值。

最后,此示例演示了 Remove 方法。

C#  复制代码 
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string
        // keys.
        SortedList<string, string> openWith = 
            new SortedList<string, string>();

// Add some elements to the list. There are no 
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is 
        // already in the list.
        try
        {
            openWith.Add("txt", "winword.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("An element with Key = \"txt\" already exists.");
        }

// The Item property is another name for the indexer, so you 
        // can omit its name when accessing elements. 
        Console.WriteLine("For key = \"rtf\", value = {0}.", 
            openWith["rtf"]);

// The indexer can be used to change the value associated
        // with a key.
        openWith["rtf"] = "winword.exe";
        Console.WriteLine("For key = \"rtf\", value = {0}.", 
            openWith["rtf"]);

// If a key does not exist, setting the indexer for that key
        // adds a new key/value pair.
        openWith["doc"] = "winword.exe";

// The indexer throws an exception if the requested key is
        // not in the list.
        try
        {
            Console.WriteLine("For key = \"tif\", value = {0}.", 
                openWith["tif"]);
        }
        catch (KeyNotFoundException)
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }

// When a program often has to try keys that turn out not to
        // be in the list, TryGetValue can be a more efficient 
        // way to retrieve values.
        string value = "";
        if (openWith.TryGetValue("tif", out value))
        {
            Console.WriteLine("For key = \"tif\", value = {0}.", value);
        }
        else
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }

// ContainsKey can be used to test keys before inserting 
        // them.
        if (!openWith.ContainsKey("ht"))
        {
            openWith.Add("ht", "hypertrm.exe");
            Console.WriteLine("Value added for key = \"ht\": {0}", 
                openWith["ht"]);
        }

// When you use foreach to enumerate list elements,
        // the elements are retrieved as KeyValuePair objects.
        Console.WriteLine();
        foreach( KeyValuePair<string, string> kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", 
                kvp.Key, kvp.Value);
        }

// To get the values alone, use the Values property.
        IList<string> ilistValues = openWith.Values;

// The elements of the list are strongly typed with the 
        // type that was specified for the SorteList values.
        Console.WriteLine();
        foreach( string s in ilistValues )
        {
            Console.WriteLine("Value = {0}", s);
        }

// The Values property is an efficient way to retrieve
        // values by index.
        Console.WriteLine("\nIndexed retrieval using the Values " +
            "property: Values[2] = {0}", openWith.Values[2]);

// To get the keys alone, use the Keys property.
        IList<string> ilistKeys = openWith.Keys;

// The elements of the list are strongly typed with the 
        // type that was specified for the SortedList keys.
        Console.WriteLine();
        foreach( string s in ilistKeys )
        {
            Console.WriteLine("Key = {0}", s);
        }

// The Keys property is an efficient way to retrieve
        // keys by index.
        Console.WriteLine("\nIndexed retrieval using the Keys " +
            "property: Keys[2] = {0}", openWith.Keys[2]);

// Use the Remove method to remove a key/value pair.
        Console.WriteLine("\nRemove(\"doc\")");
        openWith.Remove("doc");

if (!openWith.ContainsKey("doc"))
        {
            Console.WriteLine("Key \"doc\" is not found.");
        }
    }
}

/* This code example produces the following output:

An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe

Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe
Key = rtf, Value = winword.exe
Key = txt, Value = notepad.exe

Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = hypertrm.exe
Value = winword.exe
Value = notepad.exe

Indexed retrieval using the Values property: Values[2] = winword.exe

Key = bmp
Key = dib
Key = doc
Key = ht
Key = rtf
Key = txt

Indexed retrieval using the Keys property: Keys[2] = doc

Remove("doc")
Key "doc" is not found.
 */

继承层次结构
System.Object 
  System.Collections.Generic.SortedList

线程安全
此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。

只要不修改该集合,SortedList 就可以同时支持多个阅读器。即便如此,从头到尾对一个集合进行枚举本质上并不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

版本信息
.NET Framework
受以下版本支持:2.0

.NET Compact Framework
受以下版本支持:2.0

SortedList 泛型类相关推荐

  1. 类型实现《程序员的第一年》--------------C#中System.Collections.Generic.SortedDictionary 的使用...

    在改章节中,我们主要介绍类型实现的内容,自我感觉有个不错的建议和大家分享下 SortedDictionary<TKey,TValue> 类型参数 TKey 字典中的键的类型. TValue ...

  2. C# SortedDictionary以及SortedList的浅谈

    msdn叙述: The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) ...

  3. C#中的泛型 / 泛型类 / 数组、ArrayList和List三者的区别

    C#中数组.ArrayList和List三者的区别 在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的, ...

  4. 说透泛型类和泛型方法以及Class<T>和Class<?>的差异

    泛型类和泛型方法看起来似乎可以实现类似的功能,但是很多人并未真正掌握泛型方法,网上很多文章说了很多还是似是而非,特别是初学者还是搞不明白. 一.关于泛型方法 1.泛型方法可以独立于泛型类 2.泛型方法 ...

  5. Java泛型:泛型类、泛型接口和泛型方法

    2019独角兽企业重金招聘Python工程师标准>>> 根据<Java编程思想 (第4版)>中的描述,泛型出现的动机在于:有许多原因促成了泛型的出现,而最引人注意的一个原 ...

  6. C#中的Liststring泛型类示例

    在C#代码中使用一系列字符串(strings)并需要为其创建一个列表时,List<string>泛型类是一个用于存储一系列字 符串(strings)的极其优秀的解决办法.下面一起有一些Li ...

  7. Day14 自己定义泛型类的使用

    泛型的引入和体现: 问题:集合中能够存储各种类型的元素,可是由于集合能够存储各种类型数据.在获取集合中元素时,就会造成数据不安全. public class GenericDemo {public s ...

  8. 如果类是个泛型类的话dllImport 代码不能编译

    你可能需要在dllImport代码中使用泛型类.但是你知道下面的代码段能通过编译么? 1 open System.Runtime.InteropServices 2 3 type B<'T> ...

  9. 3.C#中泛型类的进一步探讨

    阅读目录 一:多重泛型  class不仅可以有T,还可以有K,实例化的时候传多个数据类型的类型,C#集合类型中的Dictionary就是多重泛型 1 using System; 2 using Sys ...

最新文章

  1. python array 使用创建10万浮点数
  2. [react] 同时引用这三个库react.js、react-dom.js和babel.js它们都有什么作用?
  3. ccleaner的专业版和商业版的注册码
  4. java threadlocal 并发_Java并发编程:ThreadLocal
  5. 大学四年规划英语计算机专业课,大学四年考证规划你get了吗?
  6. 关系抽取方法总结(基于规则-传统机器学习-深度学习)
  7. TI电量计--基本介绍及常见问题解答
  8. Oracle导入Excel中数据
  9. 带本科生做毕设是什么样的体验,看看学生是怎么评价我的
  10. 切片器可以设置日期格式?_在Power BI中设置切片器的默认值,你会吗?
  11. 《21天学通HTML+CSS+JavaScript Web开发(第7版)》——2.4 您要在Web上做什么
  12. 悲剧的与幽默的人生态度——宗白华
  13. 生成模型笔记预备知识笔记——概率分布变换
  14. 为什么《大长今》是湖南卫视引入的?
  15. 【 win10多用户登录 】win10环境下实现非企业版多用户登录
  16. 程序员都不知道的代码
  17. 错过了愚人节,还有清明节 1
  18. 最全的卫星影像分辨率和传感器参数汇总SPOT、IKONOS、QB、北京一号、ZY-1-02C
  19. 圣诞节贺卡计算机基础知识,圣诞节贺卡的优美句子大全
  20. 稀疏数组,稀疏矩阵概念

热门文章

  1. mysql 单实例部署_Mysql 数据库单机多实例部署手记
  2. vue注册新节点_vue怎么重新组装slots节点
  3. 中国剩余定理(孙子定理)的证明和c++求解
  4. 微信小程序实现滑动tab切换和点击tab切换并显示相应的数据(附源代码)
  5. NSLog打印自定义对象
  6. 无法在证书存储区中找到清单签名证书的解决办法
  7. 通往SQL Server复制的阶梯:一级- SQL服务器复制介绍
  8. 【视频点播最佳实践】视频点播播放异常排查
  9. Log4J配置方式Java工程测试
  10. poj 3321 Apple Tree