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 ...
随机推荐
- OptionsMenu
菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu),今天这讲是O ...
- 【基本技能篇】>>第2篇《如何把事情做到最好——心得》
如何把事情做到最好——全美第一本系统阐述学习与成功之道的经典著作. ——2016年2月12日 四个阅读层次:①基础阅读,具有基本阅读的能力,包括认识字,懂得词,知会句子的基本意思等.②检视阅读(也是判 ...
- 常看常遇见之一——BS架构VS CS架构
常看常遇见之一——BS架构VS CS架构 1.BS架构 即Browser/Server(浏览器/服务器)结构,是随着Internet技术的兴起,对C/S结构的一种变化或者改进的结构.在这种结构下,用户 ...
- LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1
https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...
- Bootstrap<基础二> 网格系统
Bootstrap 提供了一套响应式.移动设备优先的流式网格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. 什么是网格(Grid)? 摘自维基百科: 在平面设计中,网格 ...
- python 类以及单例模式
python 也有面向对象的思想,则一切皆对象 python 中定义一个类: class student: count = 0 books = [] def __init__(self ...
- elasticsearch相关文章
http://blog.csdn.net/laigood12345/article/category/1113868
- C#中IQueryable和IEnumberable的区别
IQueryable和IEnumberable的区别主要在查询方面有区别 IQueryable查询时间是先把skip和Take翻译成sql语句,去数据库执行完成后把数据加载到内存中 IEnumbera ...
- 两个APP跳转传值问题
最近工作上遇到个问题,新的项目要和老系统单点登录. 有点蒙,从来没做过,网上一搜都是SSO,还需要验证服务器. 仔细揣摩,其实需求很简单,没必要那么复杂,以下是我的需求和解决方案: 原系统AP1 新开 ...
- Counting Bits(Difficulty: Medium)
题目: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate ...