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 ...
随机推荐
- js判断数组
1.constructor 在W3C定义中的定义:constructor 属性返回对创建此对象的数组函数的引用 就是返回对象相对应的构造函数.从定义上来说跟instanceof不太一致,但效果都是一样 ...
- EasyUI表单内容整理
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Java集合类学习笔记(Set集合)
Set集合不允许包含相同的元素,如果试图把两个相同的元素加入同一个Set集合中,则添加操作失败,add()方法返回false,且新元素不会被加入. HashSet类的特点: 不能保证元素的排列顺序,顺 ...
- moffiestyle
听说 moffie是 带着胭脂粉气息的男人 为什么用这个名字 因为 我觉得 男生 最美 在 浓妆妖艳的时候 据说 南北朝 是 流行 男人化妆的 ...
- 【JavaScript】放大镜效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- protocol的简单写法
// // TouchView.h // LessonUIControlAndSubClass #import <UIKit/UIKit.h> @class TouchView; //1. ...
- 转-Apache的Order Allow,Deny 详解
Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是:Or ...
- Windows 7下硬盘安装Ubuntu 14.04图文教程
http://www.linuxidc.com/Linux/2014-04/100369.htm
- ajax里面success函数return上层接收不到
开发一个小功能,在success fail里直接return,没有收到返回值.排查,查了下往上的博客,参考了以下三个: http://blog.csdn.net/fairyhawk/article/d ...
- 例子:Background Audio Streamer Sample
The Background Audio Streamer sample demonstrates how to create an app that uses a MediaStreamSource ...