System.Collections.Generic的各容器类的用法
演示System.Collections.Generic的各容器类的用法.
包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSet,SortedSet,List,Queue,Stack等
System.Collections.Generic.Dictionary<>; //键/值对集合
System.Collections.Generic.KeyValuePair<>; //键/值对结构, 作为 Dictionary<> 的一个元素存在
System.Collections.Generic.SortedDictionary<>; //相当于 Key 能自动排序 Dictionary<>
System.Collections.Generic.SortedList<>; //和 SortedDictionary<> 功能相似, 但内部算法不同, 其 Keys、Values 可通过索引访问 System.Collections.Generic.HashSet<>; //无序、无重复的元素集合
System.Collections.Generic.SortedSet<>; //相当于能自动排序的 HashSet<>
System.Collections.Generic.List<>; //相当于泛型的 ArrayList, 元素可重复、可排序、可插入、可索引访问 System.Collections.Generic.Queue<>; //队列, 先进先出
System.Collections.Generic.Stack<>; //堆栈, 后进先出 System.Collections.Generic.LinkedList<>; //双向链表
System.Collections.Generic.LinkedListNode<>; //LinkedList<> 的节点 System.Collections.Generic.SynchronizedCollection<>; //线程安全的集合
System.Collections.Generic.SynchronizedReadOnlyCollection<>; //线程安全的只读集合
System.Collections.Generic.SynchronizedKeyedCollection<>; //线程安全的键/值集合 Dictionary<>、KeyValuePair<>:
protected void Button1_Click(object sender, EventArgs e)
{
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("K1", );
dict["K2"] = ;
dict.Add("K3", ); string str = "";
foreach (KeyValuePair<string, int> k in dict)
{
str += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-123; K2-456; K3-789;
}
TextBox1.Text = str;
} SortedDictionary<>:
protected void Button1_Click(object sender, EventArgs e)
{
SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
dict.Add("K3", );
dict["K1"] = ;
dict.Add("K2", ); SortedDictionary<string, int>.KeyCollection ks = dict.Keys;
SortedDictionary<string, int>.ValueCollection vs = dict.Values; string s1, s2, s3;
s1 = s2 = s3 = ""; foreach (KeyValuePair<string, int> k in dict)
{
s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
} foreach (string s in ks) { s2 += s + "; "; } //K1; K2; K3;
foreach (int n in vs) { s3 += n.ToString() + "; "; } //111; 222; 333; TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3;
} SortedList<>:
protected void Button1_Click(object sender, EventArgs e)
{
SortedList<string, int> dict = new SortedList<string, int>();
dict.Add("K3", );
dict["K1"] = ;
dict.Add("K2", ); string s1, s2, s3;
s1 = s2 = s3 = ""; foreach (KeyValuePair<string, int> k in dict)
{
s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
} s2 = dict.Keys[]; //K1
s3 = dict.Values[].ToString(); // TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3;
} HashSet<>、SortedSet<>:
protected void Button1_Click(object sender, EventArgs e)
{
HashSet<string> hs = new HashSet<string>();
hs.Add("ccc");
hs.Add("bbb");
hs.Add("aaa"); SortedSet<string> ss = new SortedSet<string>();
ss.Add("ccc");
ss.Add("bbb");
ss.Add("aaa"); string s1 = "", s2 = ""; foreach (string s in hs) { s1 += s + " "; } //ccc bbb aaa
foreach (string s in ss) { s2 += s + " "; } //aaa bbb ccc TextBox1.Text = s1 + "\\n" + s2;
} List<>:
protected void Button1_Click(object sender, EventArgs e)
{
List<int> list = new List<int>();
list.Add();
list.Add();
list.Insert(, ); string s1, s2 = "", s3, s4 = ""; s1 = list[].ToString(); //
for (int i = ; i < list.Count; i++) { s2 += list[i].ToString() + " "; } //33 11 22 list.Sort(); s3 = list[].ToString(); //
foreach (int n in list) { s4 += n.ToString() + " "; } //11 22 33 TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3 + "\\n" + s4;
} LinkedList<>、LinkedListNode<>:
protected void Button1_Click(object sender, EventArgs e)
{
LinkedList<string> list = new LinkedList<string>();
list.AddFirst("aaa");
list.AddLast("bbb");
list.AddFirst("ccc");
list.AddAfter(list.First, "ddd");
list.AddBefore(list.Last, "eee"); string s1 = "", s2 = "", s3 = "", s4 = "", s5 = ""; foreach (string s in list) { s1 += s + " "; } //ccc ddd aaa eee bbb LinkedListNode<string> node = list.First;
s2 = node.Value.ToString(); //ccc
node = node.Next;
s3 = node.Value.ToString(); //ddd
node = list.Last.Previous.Previous;
s4 = node.Value.ToString(); //aaa list.Remove("eee");
list.RemoveFirst();
list.RemoveLast(); node = list.First;
while (node != null)
{
s5 += node.Value.ToString() + " "; //ddd aaa
node = node.Next;
}
TextBox1.Text = s1 + "\\n" + s2 + "\\n" + s3 + "\\n" + s4 + "\\n" + s5;
}
System.Collections.Generic的各容器类的用法的更多相关文章
- NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>
API有一个需要实现的抽象方法: public IList<IPermission> GetPermissions(); 需要注意的是IList<IPermission>这个泛 ...
- 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1
在WPF中DataGrid 选择事件中获取SelectedItems 报错如下 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转 ...
- Unity3d:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1
问题描述:如图,在调试状态下说:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1<ignore_js_op> ...
- using System.Collections.Generic;
public class CommonClass { public static void ShowInt(int iValue) { //typeof(CommonClass) typeof关键字 ...
- webservice asmx 无法序列化接口 System.Collections.Generic.IList
转载自:http://www.cnblogs.com/chenhuzi/p/4178194.html 今天有位同事在方法里加了一个IList<entity> 的返回值,也没有测试,直接发布 ...
- Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)
在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...
- C# 经典入门12章-System.Collections.Generic命名空间
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtUAAAAsCAIAAAAl09PEAAAgAElEQVR4nOx95Vscyd7285cMPrg7Aw ...
- C# System.Collections.Generic.Dictionary
using System; using System.Collections.Generic; public class Example { public static void Main() { / ...
- 无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“System.Collections.Generic.List<Ecology.Model.EnergyFlowGraph>”
无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“Sy ...
随机推荐
- Android在代码中使用布局文件中的一个组件
使用前必须要把组件与其父组件的关系断开,比如有一个组件的名称为scrollChildLayout,则可以使用下面的代码进行分离 ((ViewGroup)scrollChildLayout.getPar ...
- zookeeper学习系列:四、Paxos算法和zookeeper的关系
一.问题起源 淘宝搜索的博客 http://www.searchtb.com/2011/01/zookeeper-research.html 提到Paxos是zookeeper的灵魂 有一篇文章标题 ...
- Excel函数汇总:
/** *D1—要查找的目标值 *G:G—查找的单元格范围,G:G表示G列 *1—查找第一个匹配 *FALSE—找到结果即返回 */ VLOOKUP(D1,G:G,1,FALSE):返回查找到的单元格 ...
- 对象列表转换为DataTable或DataTable转换为对象列表.
/**********************************************************************************/ // 说明: 数据转换工具. ...
- TEA(Tiny Encryption Algorithm)
简介 TEA是一种简单高效的加解密算法,以速度快,实现简单著称.TEA算法每一次可以操作64-bit数据,采用128-bit作为key,算法采用迭代的形式,推荐的迭代轮数是64,最少32. 代码(默认 ...
- windows内核编程 白话设备栈
在ntddk.h中定义了该函数原型: #if (NTDDI_VERSION >= NTDDI_WINXP) NTKERNELAPI NTSTATUS IoAttachDeviceToDevice ...
- 简单研究下Retrofit
2015-09-24 15:36:26 第一部分: 1. 什么是Retrofit? (点击图片有惊喜) 以上是来自官网的解释,言简意赅,咳咳,我就不翻译了~ 2. 如何使用Retrofit? 2.1 ...
- 转-深入理解VMware虚拟网络
原文出处:http://wangchunhai.blog.51cto.com/225186/381225 VMware Workstation是一款非常不错的虚拟机软件,许多爱好者用VMware Wo ...
- 多线程的学习与python实现
学习了进程与线程,现对自己的学习进行记录. 目录: 一.进程与线程的概念,以及联系与区别 二.多线程 三.python中多线程的应用 四.python实例 五.参考文献 一.进程与线程的概念.以及联系 ...
- 用Advanced Installer制作DotNetBar for Windows Forms 12.0.0.1_冰河之刃重打包版详解
关于 DotNetBar for Windows Forms 12.0.0.1_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...