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()); bool ret = list.Contains(100); Console.WriteLine(ret); ArrayList arraylist = list.GetRange(1, 3); int index = list.IndexOf(4); Console.WriteLine(index); list.Insert(1,100); list.RemoveAt(1); list.Reverse(); list.Sort(); Console.WriteLine("----------"); foreach (var item in list) { Console.WriteLine(item); } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
Hashtable
Hashtable中存放元素的信息都是乱序的
using Systemusing System.Collectionsusing System.Collections.Genericusing System.Linqusing System.Textusing System.Threading.Tasksnamespace 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]) } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
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) { 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); } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
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) { 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; 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); } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
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>(); Random random = new Random(); while (hs.Count < 100) { hs.Add(random.Next(1, 101)); } foreach (var item in hs) { Console.WriteLine(item); } } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
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() { Stack stack = new Stack(); stack.Push(1); stack.Push(2); stack.Push(3); 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()); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
队列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(); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); queue.Dequeue(); object obj = queue.Peek(); Console.WriteLine(obj + " ++++"); foreach (var item in queue) { Console.WriteLine(item); } } }}