C# 集合

SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问。

排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。

SortedList 类的方法和属性

下表列出了 SortedList 类的一些常用的 属性

属性 描述
Capacity 获取或设置 SortedList 的容量。
Count 获取 SortedList 中的元素个数。
IsFixedSize 获取一个值,表示 SortedList 是否具有固定大小。
IsReadOnly 获取一个值,表示 SortedList 是否只读。
Item 获取或设置与 SortedList 中指定的键相关的值。
Keys 获取 SortedList 中的键。
Values 获取 SortedList 中的值。

下表列出了 SortedList 类的一些常用的 方法

序号 方法名 & 描述
1 public virtual void Add( object key, object value );
向 SortedList 添加一个带有指定的键和值的元素。
2 public virtual void Clear();

从 SortedList 中移除所有的元素。
3 public virtual bool ContainsKey(
object key
);

判断 SortedList 是否包含指定的键。
4 public virtual bool ContainsValue(
object value
);

判断 SortedList 是否包含指定的值。
5 public virtual object GetByIndex(
int index
);


获取 SortedList 的指定索引处的值。

6 public virtual object GetKey(
int index
);

获取 SortedList 的指定索引处的键。
7 public virtual IList GetKeyList();

获取 SortedList 中的键。
8 public virtual IList GetValueList();

获取 SortedList 中的值。
9 public virtual int IndexOfKey(
object key
);

返回 SortedList 中的指定键的索引,索引从零开始。
10 public virtual int IndexOfValue(
object value
);

返回 SortedList 中的指定值第一次出现的索引,索引从零开始。
11 public virtual void Remove(
object key
);

从 SortedList 中移除带有指定的键的元素。
12 public virtual void RemoveAt(
int index
);

移除 SortedList 的指定索引处的元素。
13 public virtual void TrimToSize();

设置容量为 SortedList 中元素的实际个数。

实例

下面的实例演示了排序列表(SortedList)的概念:

using System;
using System.Collections; namespace CollectionsApplication
{
class Program
{
static void Main(string[] args)
{
SortedList sl = new SortedList(); sl.Add("001", "Zara Ali");
sl.Add("002", "Abida Rehman");
sl.Add("003", "Joe Holzner");
sl.Add("004", "Mausam Benazir Nur");
sl.Add("005", "M. Amlan");
sl.Add("006", "M. Arif");
sl.Add("007", "Ritesh Saikia"); if (sl.ContainsValue("Nuha Ali"))
{
Console.WriteLine("This student name is already in the list");
}
else
{
sl.Add("008", "Nuha Ali");
} // 获取键的集合
ICollection key = sl.Keys; foreach (string k in key)
{
Console.WriteLine(k + ": " + sl[k]);
}
}
}
}

当上面的代码被编译和执行时,它会产生下列结果:

001: Zara Ali
002: Abida Rehman
003: Joe Holzner
004: Mausam Banazir Nur
005: M. Amlan
006: M. Arif
007: Ritesh Saikia
008: Nuha Ali

SortedList【排序列表】的更多相关文章

  1. 集合>哈希表类Hashtable和SortedList排序列表类

    集合>哈希表类Hashtable Hashtable一种键值对的集合 ,哈希表内部的排列是无序的,而且哈希表没有提供排序方法. 集合>哈希表类Hashtable>构造普通哈希表 代码 ...

  2. C#SortedList排序列表怎么样逆序输出

    C#分别在集合库和泛型库中有共2套SortedList 以较新的泛型库为例,SortedList<int, string> l = new SortedList<int, strin ...

  3. C# 排序列表(SortedList)

    SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问. 排序列表是数组和哈希表的组合.它包含一个可使用键或索引访问各项的列表.如果您使用索引访问各项,则它是一个 ...

  4. [No000087]Linq排序,SortedList排序,二分法排序性能比较

    using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...

  5. [饭后算法系列] "头尾移动" 排序列表

    1. 问题 一个乱序列表(list), 只支持两种操作: 把一个元素移动到头部, 或者把一个元素移动到尾部. 需要设计一种算法, 使得移动次数最少而使列表有序 举两个例子: 1. {3,5,7,1,9 ...

  6. jQuery可拖拽排序列表jquery-sortable-lists

    jquery-sortable-lists可以通过鼠标进行拖动排列树型菜单,可以定义某个列表元素是否拖动,拖动后回调,点击可以折叠树型结点,可以用来在后台模仿wordpress后台拖动菜单,实现多级菜 ...

  7. Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表

    问题:根据某个或某几个字典字段来排序Python列表 answer: 通过使用operator 模块的itemgetter 函数,可以非常容易的排序这样的数据结构 eg: rows = [ {'fna ...

  8. Python 实现把两个排好序的的列表合并成一个排序列表

    列表是升序的 # -*- coding: utf-8 -*- # 合并两个排序的数组 def merge_list(a, b): if not a: return b if not b: return ...

  9. LeetCode:21_Merge Two Sorted Lists | 合并两个排序列表 | Easy

    题目:Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list sh ...

随机推荐

  1. Java基础东西(按位操作运算)

    http://aokunsang.iteye.com/blog/615658 前奏:   昨天一哥们问我Java位移你会吗,我说不会,想想位移这么麻烦,一般有位移的Java代码一律不看,有几个人会啊, ...

  2. 解读:Hadoop Archive

    hdfs并不擅长存储小文件,因为每个文件最少一个block,每个block的元数据都会在NameNode中占用150byte内存.如果存储大量的小文件,它们会吃掉NameNode节点的大量内存.MR案 ...

  3. 【前端】display: box布局教程 [转]

    css display:box 新属性   一.display:box; 在元素上设置该属性,可使其子代排列在同一水平上,类似display:inline-block;. 二.可在其子代设置如下属性 ...

  4. ubuntu 18.04 64bit下如何安装python开发工具jupyter

    1.执行一下命令进行安装 sudo apt-get install python3-distutils wget https://bootstrap.pypa.io/get-pip.py sudo p ...

  5. [CF1051F]The Shortest Statement

    题目大意:给定一张$n$个点$m$条有权边的无向联通图,$q$次询问两点间的最短路 $n\le100000$,$m\le100000$,$1\le100000$,$m$-$n\le20$. 首先看到$ ...

  6. Examining the Rooms - 第一类斯特灵数

    ---恢复内容开始--- 2017-08-10 20:32:37 writer:pprp 题意如下: Recently in Teddy's hometown there is a competiti ...

  7. 简单易用的分页类实例代码PHP

    <?php /*********************************************** * @类名: page * @参数: $myde_total - 总记录数 * $m ...

  8. ZooKeeper分布式锁简单实践

    ZooKeeper分布式锁简单实践 在分布式解决方案中,Zookeeper是一个分布式协调工具.当多个JVM客户端,同时在ZooKeeper上创建相同的一个临时节点,因为临时节点路径是保证唯一,只要谁 ...

  9. BinLog日志

    一.概述 binlog 二进制日志文件,可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事 ...

  10. winform版本自动更新

    我们在使用软件的时候经常会遇到升级版本,这也是Winform程序的一个功能,今天就大概说下我是怎么实现的吧(代码有点不完美有小BUG,后面再说) 先说下我的思路:首先在打开程序的时候去拿到我之前在网站 ...