当前位置: 首页 > 编程日记 > 正文

SortedList 泛型类

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<intstring> kvp in mySortedList) {dot.gif}
 
C
++  复制代码 
for each (KeyValuePair<int, String^> kvp in mySortedList) {dot.gif}
 
Visual Basic  复制代码 
For Each kvp As KeyValuePair(Of Integer, String) In mySortedList
    dot.gif
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<stringstring> openWith = 
            
new SortedList<stringstring>();

        
// 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<stringstring> 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();
        
foreachstring 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();
        
foreachstring 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



相关文章:

中国现代化进程专题讲座——有感

最近有上段治文老师的中国现代化进程这门课&#xff0c;感觉受益颇多。 从国外到国内&#xff0c;从古代到如今&#xff0c;讲论点、论据&#xff0c;评论历史人物、历史事件&#xff0c;讲的很宏大&#xff0c;很深刻。我并没有特意捧他&#xff0c;而是深深被其思想的深刻、言…

java运行出现JNI错误,JDK8和JDK11都安装了

java运行出现JNI错误&#xff0c;JDK8和JDK11都安装了1. 问题描述2. 尝试办法3. 解决办法3.1 解决方法&#xff1a;3.2 测试结果成功1. 问题描述 因为编程的需要&#xff0c;所以我安装了JDK8和JDK11&#xff0c;在安装好了之后配置好了环境变量&#xff0c;之后打开Eclipse的…

爱不释手(Typingfaster)1.78beta,重大升级,欢迎试用,期待反馈。

爱不释手1.78测试版主要有以下改进&#xff1a;1、改进内核&#xff0c;大幅度提高了屏显速度&#xff1b;2、增加文章分段显示功能&#xff1b;3、增加每秒按键次数统计&#xff1b;4、测试结果中划分了实际速度与名义速度&#xff0c;即实际速度&#xff1d;名义速度准确率&a…

php 网站内容采集器 Snoopy

Snoopy转载于:https://www.cnblogs.com/buxiangxin/p/7245580.html

[转]笑话: 耐力惊人的三只乌龟

某日&#xff0c;龟爸、龟妈、龟儿子三只乌龟&#xff0c;决议去郊游。带了一个山东大饼&#xff0c;和两罐海底鸡出发到XX山去。 苦爬十年&#xff0c;终於到了。席地而坐&#xff0c;卸下装备&#xff0c;准备进食。****~~~该死&#xff01;&#xff01;没带开罐器&#xff0…

如何解决代码中if…else 过多的问题

前言 if...else 是所有高级编程语言都有的必备功能。但现实中的代码往往存在着过多的 if...else。虽然 if...else 是必须的&#xff0c;但滥用 if...else 会对代码的可读性、可维护性造成很大伤害&#xff0c;进而危害到整个软件系统。现在软件开发领域出现了很多新技术、新概念…

Facial keypoints detection Kaggle 竞赛系列

3.2# Facial keypoints detection 作者&#xff1a;Stu. RuiQQ: 1026163725原文链接&#xff1a;http://blog.csdn.net/i_love_home/article/details/51051888该题主要任务是检測面部关键点位置 Detect the location of keypoints on face images 问题表述 在本问题中。要求计算…

Error:java: 无效的源发行版: 11

Error:java: 无效的源发行版: 111.问题描述2.原因查找3.解决办法3.1 打开IDEA的File—Project Structure设置3.2 修改Project SDK为自己想要切换的版本3.3 修改project languang level1.问题描述 在我的电脑中同时安装了JDK8和JDK11&#xff0c;之前本来调试好了的&#xff0c…

今天看论坛,有这样一句话,深有同感,还是家里好

就像孟宣后来对这个城市的评价&#xff1a;“这里的人活的才像人……就像那么发达国家的小城市&#xff0c;不用背负那么大的生存压力。在北京&#xff0c;如果你每天生活要30个馒头&#xff0c;那么你要挣到200到300个。而在这里&#xff0c;只需要30个馒头就可以了……”转载…

面对别人强行关机你怎么办与 定时关机

面对这个图你的第一感觉是什么?肯定是有人.....那你怎么办呢?让它继续下去!不能绝对不能!以前比较幸运的打开了几个记事本没有保存逃过了一关,可是屡试不爽呐!直到我同学出现这种情况时,幸亏我眼快,呵呵 所以问他一下!知道了这个玩意出来的命令是在运行里敲入shutdown -s如果…

iOS开发实战-基于SpriteKit的FlappyBird小游戏

写在前面 最近一直在忙自己的维P恩的事情 公司项目也是一团乱 于是...随手找了个游戏项目改了改就上线了,就当充数了. SpriteKit简介 SpriteKit是iOS 7之后苹果推出的2D游戏框架。它支持2D游戏中各种功能&#xff0c;如物理引擎&#xff0c;地图编辑&#xff0c;粒子&#xff0…

2018年12月14日 函数 总结

map() 处理序列中每个元素&#xff0c;得到迭代器&#xff0c;该迭代器 元素个数和位置与原来一致 filter() 遍历序列中的每个元素&#xff0c;判断每个元素得到布尔值&#xff0c;如果是true则留下来 people[{name:"abc","age":100},{"name":&…

UML类图新手入门级介绍

UML类图新手入门级介绍 看了大话设计模式&#xff0c;觉得很生动形象&#xff0c;比较适合于我这种初学者理解面向对象&#xff0c;所以就记录了一下。 举一个简单的例子&#xff0c;来看这样一副图&#xff0c;其中就包括了UML类图中的基本图示法。 首先&#xff0c;看动物矩形…

SQL中获取刚插入记录时对应的自增列的值

--创建数据库和表create database MyDataBaseuse MyDataBasecreate table mytable(id int identity(1,1),name varchar(20))--执行这个SQL,就能查出来刚插入记录对应的自增列的值insert into mytable values(李四)select identity转载于:https://www.cnblogs.com/bnjbl/archive…

SQL Server开发人员应聘常被问的问题妙解汇总

目前在职场中很难找到非常合格的数据库开发人员。我的一个同事曾经说过:“SQL开发是一门语言&#xff0c;它很容易学&#xff0c;但是很难掌握。” 在面试应聘的SQL Server数据库开发人员时&#xff0c;我运用了一套标准的基准技术问题。下面这些问题是我觉得能够真正有助于淘汰…

little w and Soda(思维题)

链接&#xff1a;https://ac.nowcoder.com/acm/contest/297/A 来源&#xff1a;牛客网 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 262144K&#xff0c;其他语言524288K 64bit IO Format: %lld 题目描述 不知道你听没听说过这样一个脑筋急…

[导入]实时数据库的经典书

有个朋友给我来了一封邮件&#xff0c;在邮件中&#xff0c;他这样写到&#xff1a;“国外的实时数据库来势汹汹&#xff0c;价格一路上扬&#xff1b;想当初eDNA 2003年刚到中国时也就是二、三十万左右&#xff0c;现在报价已经百万以前了。心里也总个一个结&#xff0c;难道这…

关于CSS(3)

盒子模型 盒子 盒子关系&#xff08;标准文档流&#xff09; 行内元素。 只可以设置左右外边距。 上下内边距会影响相邻的圆块状元素呢 垂直margin会合并(margin坍陷)元素嵌套的时候&#xff0c;设置子元素的上margin会被父元素抢走&#xff0c; 解决方案&#xff1a;设置父元素…

jMonkey Engine SDK3 中文乱码问题

1. 升级到了jMonkey Engine SDK 3之后出现了一些方框&#xff0c;乱码问题 官方推荐初学者使用jME3 SDK来开发游戏。官方下载地址为&#xff1a; https://github.com/jMonkeyEngine/sdk/releases 2. 问题分析和解决办法 在jME3.1.0之后SDK就有一个bug&#xff0c;菜单上的中文…

第四天上午 休闲假日

第四天晚上要离开沙巴&#xff0c;赶往吉隆坡了&#xff0c;所以这天的活动安排非常简单。 睡了一个舒服觉&#xff0c;起床吃早饭&#xff0c;我胃口还是不好&#xff0c;吃不下Magellan的美味早餐。早餐后我们来到酒店的游泳池旁休息&#xff0c;晒晒太阳、吹吹海风、看看风景…

expect--自动批量分发公钥脚本

1.在使用之前&#xff0c;先安装epel源&#xff0c;yum install expect -y2.写分发脚本&#xff0c;后缀为exp #!/usr/bin/expect set host_ip [lindex $argv 0] spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $host_ip expect {-timeout 60"(yes/no)?" { send "…

java报错MalformedURLException: unknown protocol: c

java报错&#xff1a;MalformedURLException: unknown protocol: c 1. 报错情况&#xff1a; 部分代码&#xff1a; //打开图片path"C:/Users/MyUser/image.jpg" openPictrues(path);public void openPictures(String path,String picName) throws IOException {F…

3.commonjs模块

1.首先建一个math.js exports.add function(a, b){return a b; } exports.sub function(a, b){return a - b; } exports.mul function(a, b){return a * b; } 2.然后建一个app.js 引人math.js var math require(./math); console.log(math);//{ add: [Function], sub: [Fu…

推荐一个关于.NET平台数据结构和算法的好项目

http://www.codeplex.com/NGenerics这是一个类库&#xff0c;它提供了标准的.NET框架没有实现的通用的数据结构和算法。值得大家研究。转载于:https://www.cnblogs.com/didasoft/archive/2007/07/05/806758.html

JSF和Struts的区别概述

据说JSF的主要负责人就是struts的主要作者&#xff0c;所以二者的相似点还是有很多的。 都采用taglib来处理表示层&#xff1a;在jsp页面中&#xff0c;二者都是采用一套标记库来处理页面的表示和model层的交互。 二者都采用了bean来作为和jsp页面对应的model层。该model层保存…

This和Super关键字的对比

this和Super关键字this和Super关键字的对比Super关键字的用法如下&#xff1a;1. super关键字代表了父类空间的引用&#xff1b;2. super关键字的作用&#xff1a;3. super关键字调用父类构造方法要注意的事项&#xff1a;this关键字的用法如下&#xff1a;1.了解没有 this 关键…

SQL Server 2005下的分页SQL

其实基本上有三种方法&#xff1a;1、使用SQL Server 2005中新增的ROW_NUMBER几种写法分别如下&#xff1a; 1SELECTTOP20*FROM(SELECT2ROW_NUMBER() OVER(ORDERBYNamec) ASRowNumber,3*4FROM5dbo.mem_member) _myResults6WHERE7RowNumber >1000081SELECT*FROM(SELECT2ROW_N…

Oozie 配合 sqoop hive 实现数据分析输出到 mysql

文件/RDBMS -> flume/sqoop -> HDFS -> Hive -> HDFS -> Sqoop -> RDBMS 其中&#xff0c;本文实现了 使用 sqoop 从 RDBMS 中读取数据(非Oozie实现&#xff0c;具体错误将在本文最后说明)从 Hive 处理数据存储到 HDFS使用 sqoop 将 HDFS 存储到 RDBMS 中 1.…

关于eclipse的注释和反注释的快捷键

使用eclipse那么久了额&#xff0c;对注释和反注释的快捷键一直很模糊&#xff0c;现在记下来&#xff0c;方便查看。 注释和反注释有两种方式。如对下面这段代码片段&#xff08;①&#xff09;进行注释&#xff1a; private String value; private String count; public voi…

DNN和IBatis.Net几乎同时发布新版本

DotNetNuke发布了最新的版本4.5.0&#xff0c;确实让人期待了很久&#xff0c;据说这个版本在性能上有很大的提升。 IBatis.NET几乎在同一时间也发布了新版本DataMapper 1.6.1&#xff0c;也有不少的改进。 项目中使用到的这两个东西几乎同时发布新版本&#xff0c;振奋人心啊&…