C#编程入门_常用集合_14
来源:互联网 发布:上瘾网络剧猛其其花絮 编辑:程序博客网 时间:2024/05/19 04:52
21篇C#博客的配套源码
ArrayList
ArrayList中可以添加任意类型的数据,但是在获取数据的时候有时候会麻烦一些
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ArrayListDemo{ class Program { static void Main(string[] args) { ArrayList list = new ArrayList(); //获取集合元素的数量 Console.WriteLine(list.Count); //向集合中添加数据 list.Add(2); list.Add(3); list.Add(4); list.Add(5); list.Add(6); list.Add(7); Console.WriteLine(list.Capacity); list.Add(new Student()); //向集合中添加一个新的集合 //list.AddRange(list); //list集合中是否包含100 bool ret = list.Contains(100); Console.WriteLine(ret); //从下标为1的位置取3个元素做为新的结合返回 ArrayList arraylist = list.GetRange(1, 3);// 3 4 5 //foreach 是对集合做修改 遍历操作 禁止对foreach做删除操作 //foreach (var item in arraylist) //{ // if ((int)item == 4) // { // list.Remove(item); // } //} //返回元素为4所在的下标 int index = list.IndexOf(4); Console.WriteLine(index); //在下标为1的位置插入100 list.Insert(1,100); //删除指定下标的元素 list.RemoveAt(1); //反转 list.Reverse(); //排序 list.Sort(); Console.WriteLine("----------"); foreach (var item in list) { Console.WriteLine(item); } } }}
Hashtable
Hashtable中存放元素的信息都是乱序的
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace HashtableDemo{ class Program { static void Main(string[] args) { //在 Hashtable 这样的集合中 存储的顺序是乱序的 Hashtable ht = new Hashtable(); ht.Add("北京","重度雾霾"); ht.Add("上海", "梅雨"); ht.Add("郑州","霾"); Console.WriteLine(ht["北京"]); ht["石家庄"] = "123"; bool ret = ht.Contains("石家庄"); Console.WriteLine(ret); //移除键为郑州的项 //ht.Remove("郑州"); //集合中是否包含键为"郑州"的值 ret = ht.Contains("郑州"); Console.WriteLine(ret); //对集合遍历 foreach (var item in ht.Keys) { Console.WriteLine(item +" "+ht[item]); } } }}
List泛型集合
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 泛型结合{ class Program { static void Main(string[] args) { //ArrayList list = new ArrayList(); ////Add方法的参数类型是object类型 在传递参数的时候整型1会转化成object类型 这样属于装箱操作 //list.Add(1); ////如果实现 list[0] + 2 得到正确的结果 要将list[0]转化成int类型 也就是说要进行拆箱操作 //Console.WriteLine((int)list[0] + 2); //泛型结合在声明的时候已经确定了里面的元素类型 List<string> list = new List<string>(); //里面的传入的数据类型只能和声明时定义的类型保持一致 //泛型能在编译时,提供强大的类型检查,减少数据类型之间的显示转化、装箱操作和运行时的类型检查。 list.Add("C#编程之道"); list.Add("C#从入门到精通"); list.AddRange(list); Console.WriteLine(list.Count); bool ret = list.Contains("C#编程之道"); Console.WriteLine(ret); int index = list.IndexOf("C#从入门到精通", 2); Console.WriteLine(index); List<int> numList = new List<int>(); numList.Add(1); numList.Add(2); numList.Add(5); numList.Add(1); numList.Add(2); numList.Add(2); numList.Add(4); List<int> newList = numList.GetRange(0, numList.Count); index = -1; //记录查找元素的数量 int count = 0; while (newList.IndexOf(2) != -1) { index = newList.IndexOf(2); count++; newList.RemoveAt(index); } Console.WriteLine(count); Console.WriteLine("-------------------------"); foreach (var item in numList) { Console.WriteLine(item); } } }}
Dictionary泛型结合
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using static System.Collections.Generic.Dictionary<string, string>;namespace 字典{ class Program { static void Main(string[] args) { //Test01(); //Test02(); Test03(); } static void Test01() { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("张杰", "高飞"); dic.Add("刘欢", "好汉歌"); //在一个字典中 键是唯一的 //在一个字典中不同的键可以对应相同的值 dic.Add("周杰伦", "青花瓷"); dic.Add("刘英", "青花瓷"); bool key = dic.ContainsKey("周杰伦"); Console.WriteLine(key); bool value = dic.ContainsValue("好汉歌"); Console.WriteLine(value); KeyCollection keys = dic.Keys; //dic.Remove("刘英"); foreach (var item in keys) { Console.WriteLine(item); } } static void Test02() { Dictionary<string, Book> books = new Dictionary<string, Book>(); books.Add("0000000",new Book("C#编程之道",56,"王垚")); books.Add("0000001", new Book("C#从入门到精通", 98, "YY")); books.Add("0000002", new Book("C#从入门到放弃", 2, "亚东")); foreach (var item in books) { Console.WriteLine(item.Key +" " + item.Value.Price); } } //有序字典 static void Test03() { SortedDictionary<string, string> dic = new SortedDictionary<string, string>(); dic.Add("张杰", "高飞"); dic.Add("刘欢", "好汉歌"); //在一个字典中 键是唯一的 //在一个字典中不同的键可以对应相同的值 dic.Add("周杰伦", "青花瓷"); dic.Add("刘英", "青花瓷"); dic.Add("asdef", "青花瓷"); dic.Add("asdeh", "青花瓷"); bool key = dic.ContainsKey("周杰伦"); Console.WriteLine(key); bool value = dic.ContainsValue("好汉歌"); Console.WriteLine(value); foreach (KeyValuePair<string,string> item in dic) { Console.WriteLine(item.Key); } } }}
HashSet泛型集合
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace HashSet{ class Program { static void Main(string[] args) { HashSet<int> hs = new HashSet<int>(); //hs.Add(1); //Console.WriteLine(hs.Count); //hs.Add(2); //Console.WriteLine(hs.Count); //hs.Add(1); //Console.WriteLine(hs.Count); //HashSet是不能重复添加相同的元素的 //在结合中添加100个1-100的随机数 Random random = new Random(); while (hs.Count < 100) { hs.Add(random.Next(1, 101)); } foreach (var item in hs) { Console.WriteLine(item); } } }}
Stack 栈
栈是一种先进后出的集合
using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 栈{ class Program { static void Main(string[] args) { Console.WriteLine(Test02(30, 26)); } static void Test() { //FILO first in last out Stack stack = new Stack(); //将数据压入到栈 stack.Push(1); stack.Push(2); stack.Push(3); //Console.WriteLine(stack.Count); //弹出栈顶数据 就是删除栈顶元素 stack.Pop(); //获取栈顶元素 object numberObj = stack.Peek(); Console.WriteLine(numberObj); foreach (var item in stack) { Console.WriteLine(item); } } static string Test02(int number,int baseNumber) { Stack<char> statck = new Stack<char>(); while (number/baseNumber !=0) { statck.Push( (char)(number % baseNumber +'a')); number /= baseNumber; } statck.Push((char)(number + 'a')); return new string(statck.ToArray()); } }}
队列Queue
队列是先进先出的 使用队列做类似约瑟夫环的题目是比较方便的
using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 队列{ class Program { static void Main(string[] args) { Queue queue = new Queue(); //EnterQueue 入队 queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); //DeleteQueue queue.Dequeue(); //peek 不影响队列中的元素的 object obj = queue.Peek(); Console.WriteLine(obj + " ++++"); foreach (var item in queue) { Console.WriteLine(item); } } }}
阅读全文
0 0
- C#编程入门_常用集合_14
- C#编程入门14_常用集合
- C#之入门总结_进程,线程_14
- 黑马程序员_Java基础_集合框架(一)_14
- C#之入门总结_集合_10
- C#编程入门_网络编程_21
- C#编程入门21_网络编程
- C#与Matlab混合编程_入门
- C#编程入门_循环_5
- C#编程入门_数组_6
- C#编程入门_方法(函数)_7
- C#编程入门_跳转语句_8
- C#编程入门_杂项_12
- C#编程入门_简单算法_15
- C#编程入门_异常处理_16
- C#编程入门_文件处理IO_17
- C#编程入门_反射_19
- C#编程入门5_循环
- C++7.18
- git出现 *** Please tell me who you are. Run...... 错误
- CRC校验算法
- JVM内存区域划分(JDK6 7 8中的变化)
- css的三大特征
- C#编程入门_常用集合_14
- Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
- 设计模式(7)--桥接模式
- HDU-3938 Portal 克鲁斯卡尔思想 (离线并查集)
- 详解系统中的环境变量
- 【刷题剑指offer】字符串的排列
- CountDownLatch与CyclicBarrier
- iOS编程学习十一
- Nginx 配置文件 nginx.conf 详解