命名空间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();        }    }}

  程序输出结果:

转载于:https://www.cnblogs.com/mcgrady/archive/2012/03/03/2376224.html

你能熟练使用Dictionary字典和List列表吗?相关推荐

  1. 关于Dictionary字典和List列表

    命名空间System.Collections.Generic中有两个非常重要,而且常用的泛型集合类,它们分别是Dictionary<TKey,TValue>字典和List<T> ...

  2. List数组,string数组,Dictionary字典三种contain方法的查询速度

    在生成随机不重复数时要判断生成的数是否已生成过,这时就要和原来生成的数进行比较是否有重复,有以下三种方法 1. list数组采用contains()方法 2.string数组采用contains()方 ...

  3. C#中的Dictionary字典类介绍

    关键字:C# Dictionary 字典  作者:txw1958 原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-diction ...

  4. swift_005(Swift的Dictionary 字典)

    1.Dictionary (可变不可变不区分了)字典  <Swift开发指南> // Swift 字典的key没有类型限制可以是整型或字符串,但必须是唯一的. var someDict:[ ...

  5. boost::contract模块实现dictionary字典的测试程序

    boost::contract模块实现dictionary字典的测试程序 实现功能 C++实现代码 实现功能 boost::contract模块实现dictionary字典的测试程序 C++实现代码 ...

  6. C#中的Dictionary字典类介绍(转载)

    C#中的Dictionary字典类介绍 关键字:C# Dictionary 字典  作者:txw1958 原文:http://www.cnblogs.com/txw1958/archive/2012/ ...

  7. (转)C#中的Dictionary字典类介绍

    关键字:C# Dictionary 字典  作者:txw1958 原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-diction ...

  8. 英文单词缩写----DXNRY – Dictionary 字典

    由于英文的字母太多,不方便的时候发短信很慢,所以外国人很多都在短信上输入 缩写 ,这样就节省了很多时间.花了好久才把这些 缩写 给整理出来,唯一在这里需要说明的是,不是所有的人都明白这些 缩写 是什么 ...

  9. 【Python】数据类型 Number数字、String字符串、List列表、Tuple元组、Set集合、Dictionary字典

    Python数据类型 2021/6/3 周四 学习内容: Python数据类型.Number(数字).String(字符串).List(列表).Tuple(元组).Set(集合).Dictionary ...

最新文章

  1. 连接两个点云中的字段或数据形成新点云以及Opennni Grabber初识
  2. 【BZOJ2117】 [2010国家集训队]Crash的旅游计划
  3. is属性用法 vue_vue组件讲解(is属性的用法)模板标签替换操作
  4. [转载]为什么使用 SLF4J 而不是Log4J来做Java 日志
  5. swift自行车品牌介绍_品牌101:简介
  6. 前端学习(2854):简单秒杀系统学习之settimeout
  7. 《MPLS在Cisco IOS上的配置》一第 1 章 MPLS概述1.1 IP转发过程概述
  8. 那一年马云34岁,李彦宏30岁,马化腾27岁
  9. 安卓机器人+淘宝客介绍
  10. C 实现黑客帝国数字雨
  11. 如何利用官方SDK文件来辅助开发?
  12. 宋浩《概率论与数理统计》自用笔记
  13. Java期末考试试题及参考答案(01)
  14. shell编程之sed
  15. Schnorr签名体制
  16. 时间序列数据集:UCR Time Series Classification Archive【共128个数据集】
  17. msgpack-c 官方文档整理翻译之pack
  18. jzoj 1420. 佳肴
  19. VIBE君,放过MOTO吧
  20. Windows 10 修改 Alt+Tab 键 切换 新版 Microsoft Edge 单个标签页/窗口

热门文章

  1. 设系统中有三种类型的资源(A,B,C)的五个进程(P1,P2,P3,P4,P5)。A资源的数量为17,B资源的数量为5,C资源的数量为20。在T0时刻系统状态如表所示。
  2. 3376: [Usaco2004 Open]Cube Stacking 方块游戏(带权并查集)
  3. 虚树(bzoj 3572: [Hnoi2014]世界树)
  4. bzoj 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏(floyd)
  5. multsim仿真1:利用multisim化简逻辑函数
  6. 贺利坚老师汇编课程51笔记:MUL乘法指令
  7. Exponent CMS 2.3.9 配置文件写入 Getshell分析
  8. centos下 将(jgp、png)图片转换成webp格式
  9. Eclipse中文乱码解决汇总(应该比较全):
  10. GO语言练习:网络编程 ICMP 示例