你能熟练使用Dictionary字典和List列表吗?(转)
命名空间System.Collections.Generic中有两个非常重要,而且常用的泛型集合类,它们分别是Dictionary<TKey,TValue>字典和List<T>列表。Dictionary字典通常用于保存键/值对的数据,而List列表通中用于保存可通过索引访问的对象的强类型列表。下面来总结一下,用代码来演示怎么初始化,增加,修改,删除和遍历元素。
Dictionary<TKey,TValue>字典
代码如下:

namespace DictionaryDemo1
{
class Program
{
static void Main(string[] args)
{
//创建Dictionary<TKey,TValue>对象
Dictionary<string, string> openWith = new Dictionary<string, string>(); //添加元素到对象中,共有两种方法。注意,字典中的键不可以重复,但值可以重复。
//方法一:使用对象的Add()方法
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe"); //方法二:使用索引器Indexer
//openWith["txt"] = "notepad.exe";
//openWith["bmp"] = "paint.exe";
//openWith["dib"] = "paint.exe";
//openWith["rtf"] = "wordpad.exe"; //增加元素,注意增加前必须检查要增加的键是否存在使用ContainsKey()方法
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");//或openWith["ht"] = "hypertrm.exe";
Console.WriteLine("增加元素成功!Key={0},Value={1}","ht",openWith["ht"]);
} //删除元素,使用Remove()方法
if (openWith.ContainsKey("rtf"))
{
openWith.Remove("rtf");
Console.WriteLine("删除元素成功!键为rtf");
} if (!openWith.ContainsKey("rtf"))
{
Console.WriteLine("Key=\"rtf\"的元素找不到!");
} //修改元素,使用索引器
if (openWith.ContainsKey("txt"))
{
openWith["txt"] = "notepadUpdate.exe";
Console.WriteLine("修改元素成功!Key={0},Value={1}", "txt", openWith["txt"]);
} //遍历元素,因为该类实现了IEnumerable接口,所以可以使用foreach语句,注意元素类型是 KeyValuePair(Of TKey, TValue)
foreach (KeyValuePair<string, string> kvp in openWith)
{
Console.WriteLine("Key={0},Value={1}",kvp.Key,kvp.Value);
} Console.WriteLine("遍历元素完成!");
Console.ReadKey();
}
}
}

程序输出结果:

List<T>列表
代码如下:

namespace ListDemo1
{
class Program
{
static void Main(string[] args)
{
//创建List<T>列表对象
List<string> dinosaurs = new List<string>(); //增加元素到列表(或称为初始化),注意初始化时不能使用索引器,因为没有增加元素之前list列表是空的
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus"); //一个重要属性
Console.WriteLine("列表中的元素数为: {0}", dinosaurs.Count);//获取 List 中实际包含的元素数 //插入元素,使用Insert()方法
dinosaurs.Insert(2, "Compsognathus");//将元素插入到指定索引处,原来此位置的元素后移
Console.WriteLine("在索引为2的位置插入了元素{0}",dinosaurs[2]); //删除元素,使用Remove()方法
dinosaurs.Remove("Compsognathus");//从 List 中移除特定对象的第一个匹配项
Console.WriteLine("删除第一个名为Compsognathus的元素!"); //修改元素,使用索引器
dinosaurs[0] = "TyrannosaurusUpdate";
Console.WriteLine("修改索引为0的元素成功!"); //遍历元素,使用foreach语句,元素类型为string
foreach (string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
} Console.WriteLine("遍历元素完成!");
Console.ReadKey();
}
}
}

程序输出结果:

你能熟练使用Dictionary字典和List列表吗?(转)的更多相关文章
- 关于Dictionary字典和List列表
命名空间System.Collections.Generic中有两个非常重要,而且常用的泛型集合类,它们分别是Dictionary<TKey,TValue>字典和List<T> ...
- (转)C#中的Dictionary字典类介绍
关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...
- C#中的Dictionary字典类介绍
Dictionary字典类介绍 必须包含名空间System.Collection.Generic Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是 ...
- dictionary(字典)
dictionary(字典): 字典对象 字典是一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划.字母来查对应页的详细内容. 1. dic={"n ...
- C# Dictionary 字典
C#中的Dictionary字典类介绍 关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/ ...
- Python dictionary 字典 常用法
Python dictionary 字典 常用法 d = {} d.has_key(key_in) # if has the key of key_in d.keys() ...
- 上传程序Dictionary 字典 哈希--多读一写锁ReaderWriterLock
//上传程序Dictionary 字典 哈希 /// <summary> /// 车辆控制信息哈斯表,Key是终端号,Value是车辆信息控制对象 /// </summary> ...
- 04.Dictionary字典键值对集合
Dictionary字典键值对集合和Hashtable键值对集合的功能非常类似, 只是在声明的时候,必须为其制定值的类型. 示例代码: namespace _11.Dictionary字典集合的学习 ...
- JavaScript数据结构——集合、字典和散列表
集合.字典和散列表都可以存储不重复的值. 在集合中,我们感兴趣的是每个值本身,并把它当作主要元素.在字典和散列表中,我们用 [键,值] 的形式来存储数据. 集合(Set 类):[值,值]对,是一组由无 ...
随机推荐
- 预写式日志(Write-Ahead Logging (WAL))
SQL Server中使用了WAL(Write-Ahead Logging)技术来保证事务日志的ACID特性.而且大大减少了IO操作. WAL的核心思想是:在数据写入到数据库之前,先写入到日志.再将日 ...
- Oracle表字段的增加、删除、修改和重命名
本文主要是关于Oracle数据库表中字段的增加.删除.修改和重命名的操作. 增加字段语法:alter table tablename add (column datatype [default val ...
- IP地址(IPv4)/IPv6地址的正则表达式
原地址:http://pfeishao.blog.163.com/blog/static/18162337020112113130453/ Pv4地址正则表达式:^((25[0-5]|2[0-4]\d ...
- 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式
在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式 转载自:http://blog.163.com/smhily_min/blog/static/75206226201092011 ...
- CentOS7 安装MySQL
从官网下载源再用yum安装: # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -ivh m ...
- 理解 OpenStack + Ceph (5):OpenStack 与 Ceph 之间的集成 [OpenStack Integration with Ceph]
理解 OpenStack + Ceph 系列文章: (1)安装和部署 (2)Ceph RBD 接口和工具 (3)Ceph 物理和逻辑结构 (4)Ceph 的基础数据结构 (5)Ceph 与 OpenS ...
- C++浅析——虚表和虚表Hook
为了探究虚表的今生前世,先来一段测试代码 虚函数类: class CTest { public: int m_nData; virtual void PrintData() { printf(&quo ...
- html5新增及废除属性
html5中,在新增加和废除很多元素的同时,也增加和废除了很多属性. 一.新增属性 1.表单属性 a.autofocus 对input[所有类型].select.textarea与button指定au ...
- Lucene 4.x Spellcheck使用说明
Spellcheck是Lucene新版本的功能,在介绍spellcheck之前,我们需要弄清楚Spellcheck支持几种数据源.Spellcheck构造函数需要传入Dictionary接口: pac ...
- POJ1837 Balance[分组背包]
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13717 Accepted: 8616 Descript ...