你能熟练使用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 类):[值,值]对,是一组由无 ...
随机推荐
- 检测PC端和移动端的方法之一
window.mobileCheck = function() { var check = false; (function(a){if(/(android|bb\d+|meego).+mobile| ...
- CKEditor与CKFinder的配置(ASP.NET环境)
CKEditor是一个专门使用在网页上的所得文字编辑器,适用于PHP.ASP.NET.Java等后端开发语言.CKEditor原名为FCKeditor,“FCK” 是这个编辑器的作者的名字Freder ...
- 点击jqGrid表格,弹出需要的表格的数据
首先,我们先定义一个函数,然后在JQuery里面直接引用就可以了, function GetJqGridRowValue(jgrid, code) { var KeyValue = "&qu ...
- c# bass入门学习
据说bass挺好用的,所以又搞了个音乐播放器 这是参考了别人的bass教学结合自己的理解写的bass操作类 public class BassHelper { private static BassH ...
- Spark大数据的学习历程
Spark主要的编程语言是Scala,选择Scala是因为它的简洁性(Scala可以很方便在交互式下使用)和性能(JVM上的静态强类型语言).Spark支持Java编程,但对于使用Java就没有了Sp ...
- <a>标签,鼠标经过或者停留触发延时响应事件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcUAAAEoCAIAAACmeX2PAAAgAElEQVR4nOzdd3xUdb74f3+Pu3v33t ...
- ps, top, pstree
ps 查看当前终端所启动的进程, 不加选项只查看当前终端的进程 PID TTY TIME CMD 2398 pts/1 00:00:00 bash 3625 pts/1 00:00:00 ps #PI ...
- JAVA-android 更改APP名称与图标
首先要在你的资源文件放入你想换的图标图片拖到drawable-XX文件夹下,然后你打开AndroidManifest.xml这个配置清单文件找到application标签里的这句android:ico ...
- 一:Go编程语言规范--块、声明、作用域
1.块 块为一对大括号括住的声明和语句.块 = "{" { 语句 ";" } "}" . 除显式源码块外,还有隐式块: 全域块 包含所有的G ...
- AngularJS性能优化
几个概念 域$scope和更新周期DigestCycle AngularJS的域本质上是一些JavaScript对象,它们从一些预定义的对象继承而来.基本上,小的域比大的域运行要快. 每创建一个新的域 ...