C#集合类用法实例代码详解
时间:2019-04-07
本文章向大家介绍C#集合类用法实例代码详解,主要包括C#集合类用法实例代码详解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
下面介绍C#的集合类
1ArrayList
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace 动态数组ArrayList { class Program { static void Main(string[] args) { ArrayList a1 = new ArrayList(); a1.Add(100); foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 }) { a1.Add(number); } int[] number2 = new int[2] { 11, 12 }; a1.AddRange(number2); a1.Remove(3); a1.RemoveAt(3); ArrayList al2 = new ArrayList(a1.GetRange(1,3)); Console.WriteLine("遍历方法1:"); foreach (int i in a1) { Console.WriteLine(i); } Console.WriteLine("遍历方法2:"); for (int i = 0; i < al2.Count; i++) { Console.WriteLine(al2[i]); } Console.ReadLine(); } } }
2 Stack
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace Stack集合类 { class Program { static void Main(string[] args) { Stack s1 = new Stack(); Stack s2 = new Stack(); foreach (int i in new int[4] { 1, 2, 3, 4 }) { s1.Push(i); s2.Push(i); } foreach (int i in s1) { Console.WriteLine(i); } s1.Pop(); Console.WriteLine("出栈"); foreach (int i in s1) { Console.WriteLine(i); } int num=(int)s2.Peek(); Console.WriteLine("弹出最后一项{0}",num); foreach (int i in s2) { Console.WriteLine(i); } Console.ReadLine(); } } }
3Queue
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace Queue集合类 { class Program { static void Main(string[] args) { Queue q1 = new Queue(); Queue q2 = new Queue(); foreach(int i in new int [4]{1,2,3,4}) { q1.Enqueue(i); q2.Enqueue(i); } foreach (int i in q1) { Console.WriteLine(i); } q1.Dequeue(); Console.WriteLine("q1出队"); foreach (int i in q1) { Console.WriteLine(i); } int num=(int)q2.Peek(); Console.WriteLine("取q2开始处{0}",num); foreach(int i in q2) { Console.WriteLine(i); } Console.ReadLine(); } } }
4Hashtable
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace Hashtable集合类 { class Program { static void Main(string[] args) { Hashtable h = new Hashtable(); h.Add("E","e"); h.Add("B", "b"); h.Add("C", "c"); h.Add("A", "a"); foreach (DictionaryEntry e in h) { Console.Write("{0},{1} ", e.Key, e.Value); } Console.WriteLine(); string s = (string)h["C"]; Console.WriteLine(s); if (h.Contains("E")) { Console.WriteLine("含有E"); } Console.WriteLine(h["A"]); h.Remove(h["A"]); h.Clear(); foreach (DictionaryEntry e in h) { Console.Write("{0},{1} ", e.Key, e.Value); } Console.ReadLine(); } } }
5SortedList
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace SortedList集合类 { class Program { static void Main(string[] args) { SortedList s1 = new SortedList(); s1["c"]=41; s1["a"]=42; s1["d"]=11; s1["b"]=13; foreach (DictionaryEntry e in s1) { string s = (string)e.Key; int i = (int)e.Value; Console.Write("{0},{1} ",s,i); } Console.ReadLine(); } } }
总结
以上所述是小编给大家介绍的C#集合类用法实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
- HDU 5144 NPY and shot(物理运动学+三分查找)
- 深度|Python股票数据分析
- HDU 2438 Turn the corner(三分查找)
- UVAlive 3708 Graveyard(最优化问题)
- HDU 1754 I Hate It(线段树之单点更新,区间最值)
- Selenium2+python自动化19-单选和复选框
- Uva 11300 Spreading the Wealth(递推,中位数)
- Uva 11729 Commando War (简单贪心)
- UVA 11292 Dragon of Loowater(简单贪心)
- Codeforces Beta Round #2 A,B,C
- 牛顿迭代法(Newton's Method)
- 最长递减子序列(nlogn)(个人模版)
- Selenium2+python自动化26-js处理内嵌div滚动条
- Selenium2+python自动化25-js处理日历控件
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- java的反射机制到底是做什么的?
- Java中是否直接可以使用enum进行传输
- PHP 恶意程序简单分析
- springboot之相关环境设置
- springboot之第一个springboot程序
- 「查缺补漏」巩固你的RocketMQ知识体系
- springboot之场景启动器
- ICLR2020 | 深度自适应Transformer
- springboot之自动配置
- golang--连接redis数据库并进行增删查改
- golang--redis连接池
- springboot配置之使用application.properties时编码问题
- mybatis动态sql之foreach补充(二)
- golang数据结构之稀疏数组
- mybatis扩展之使用PageHelper插件进行分页(是真好用)