演示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的各容器类的用法的更多相关文章

  1. NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>

    API有一个需要实现的抽象方法: public IList<IPermission> GetPermissions(); 需要注意的是IList<IPermission>这个泛 ...

  2. 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1

    在WPF中DataGrid 选择事件中获取SelectedItems 报错如下 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转 ...

  3. Unity3d:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1

    问题描述:如图,在调试状态下说:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1<ignore_js_op> ...

  4. using System.Collections.Generic;

    public class CommonClass { public static void ShowInt(int iValue) { //typeof(CommonClass) typeof关键字 ...

  5. webservice asmx 无法序列化接口 System.Collections.Generic.IList

    转载自:http://www.cnblogs.com/chenhuzi/p/4178194.html 今天有位同事在方法里加了一个IList<entity> 的返回值,也没有测试,直接发布 ...

  6. Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)

    在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...

  7. C# 经典入门12章-System.Collections.Generic命名空间

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtUAAAAsCAIAAAAl09PEAAAgAElEQVR4nOx95Vscyd7285cMPrg7Aw ...

  8. C# System.Collections.Generic.Dictionary

    using System; using System.Collections.Generic; public class Example { public static void Main() { / ...

  9. 无法将类型“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 ...

随机推荐

  1. 通过JS检测360浏览器

    如何通过JS检测360浏览器? 尝试了一大堆方法,网上大多数办法都是通过navigator.userAgent来判断,这可能在几年前是行得通的,现在360userAgent输出来跟谷歌除了版本号其余一 ...

  2. java sqlhelper

    dbinfo.properties部分: 注意每行末尾不可以有空格 #oracle configure UserName=scott Password=tiger Driver=oracle.jdbc ...

  3. 数位DP入门

    HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...

  4. css3 transfrom变换

    Transform 转换 CSS3中的转换允许我们对元素进行旋转.缩放.移动或倾斜,它分为2D转换 或3D转换 在CSS2时代,如果要做一些图片转换角度,都依赖于图片.flash或JavaScript ...

  5. 手机客户端UI测试常见的测试点

    1.各种分辨率下,显示正常.现市场上主流的塞班V3系统手机为240*320.320*240.WM系统主要为240*320.320*480.Android系统主要为320*480,Iphone系统为32 ...

  6. mybatis-generator-config工具的使用

    generator.xml <?xml version="1.0" encoding="UTF-8" ?>   <!DOCTYPE gener ...

  7. Topcoder SRM558 1000 SurroundingGame

    题意:给定一个网格,每个网格有选取代价和占据收益.每个点被占据,需要满足以下两个条件至少一个条件:1.被选取  2.邻近方格都被选取(有公共边被称为邻近)  不一定要占据所有方格,求最大收益. 第一直 ...

  8. css个人随笔,适合新手总结整理

    CSS的3种引用方式:1.外部样式表 都是在head标签内使用Link标签来引用的.2.内部样式表 <style type="text/css"> </style ...

  9. PHP的后期静态绑定

    self 是个孝子 不管后来 谁是它的领导(调用它)  谁生了它  它就听谁的 子类调用父类的方法 self 的生存空间是父类 不管是不是子类调用 我生在哪 我就在哪个类里面找属性/方法 static ...

  10. Eclipse 项目中有红色感叹号,怎么办?

    /** * JDK1.5中增加了自动拆装箱的语言特性,在基本类型和包装类型之间可以相互地转换和运算. * 大家也都知道Java中==运算符是比较两个对象间的引用是否相同.在自动拆装箱与“==”运算符之 ...