你能熟练使用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 类):[值,值]对,是一组由无 ...
随机推荐
- 问题解决——开启Guest后仍无法共享打印机
==================================声明================================== 本文版权归作者所有 未经作者授权 请勿转载 保留法律追究的 ...
- iOS打包Framework真机和模拟器兼容合并版本 - 详细攻略步骤
打包Framework,测试时: 1.用模拟器打包,测试时只能跑在模拟器 2.用真机打包,测试时只能跑在真机 那么怎么做到一个版本兼容以上两种场景呢? 解决如下: 1.打开终端 2.输入 lipo ...
- my_ls
#include<stdio.h> #include<dirent.h> #include<string.h> #include<sys/types.h> ...
- 3.Docker - 镜像管理
一.使用容器生成镜像 1.进入容器,安装软件包(任意包) 1 2 3 4 5 6 7 bash-3.2# docker ps -a CONTAINER ID IMAGE ...
- 在Eclipse里查看Java字节码
要理解 Java 字节码,比较推荐的方法是自己尝试编写源码对照字节码学习.其中阅读 Java 字节码的工具必不可少.虽然javap可以以可读的形式展示出.class 文件中字节码,但每次改动源码都需调 ...
- tcp选项TCP_DEFER_ACCEPT
tcp选项TCP_DEFER_ACCEPT http://blog.chinaunix.net/uid-23207633-id-274317.html 之前在项目测试的时候,如果第三次握手发完裸ack ...
- hadoop如何处理长时间运行不完成的map/reduce 任务?
如果某一个任务在某个节点上长时间不完成,怎么手动干预来处理这种情况?董西成博客上找到的回答:hadoop中有三种特殊的任务,failed task,killed task和speculative ta ...
- Rename in Batch [Python]
#!/usr/bin/python2.7 # Program: # Rename files in current folder in batch. # Date: # 2016-04-17 # Us ...
- Caffe fine-tuning 微调网络
转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ 目前呢,caffe,theano,torch是当下比较流行的De ...
- JSTL基础
是java中的一个定制标记库集 实现了jsp页面中的代码复用,基于标签库原理,重复率较高的代码块支持复用,提高效率 书写jsp页面时可读性更强 http://archive.apache.org/di ...