ArrayList和HashTable集合
1.ArrayList集合
***添加元素
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { // ArrayList list = new ArrayList(); //集合:很多数据的一个集合,长度可以任意改变,类型随便 //数组:长度不可变,类型单一 list.Add(1); list.Add(true); list.Add("zq"); list.Add('w'); /* * ToString方法 我们将一个对象输出到控制台 默认情况下 打印的就是这个对象所在的类的命名空间 */ list.Add(new int []{ 1,2,3,4,5,6,7}); Person p = new Person(); list.Add(p); list.Add(list); for(int i=0;i
***添加集合元素
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;using System.IO;namespace ArrayList{ class Program { static void Main(string[] args) { System.Collections.ArrayList list = new System.Collections.ArrayList(); list.Add(123); //添加集合元素 list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); list.AddRange(list); for(int i=0;i
***集合的操作(插入,删除,清空,反转,排序)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;using System.IO;namespace ArrayList{ class Program { static void Main(string[] args) { System.Collections.ArrayList list = new System.Collections.ArrayList(); list.Add(123); list.Add(true); //添加集合元素 list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); list.AddRange(list); //******************************* //list.Clear();//清空所以元素 //list.Remove(true);//删除单个元素 //list.RemoveAt(0);//根据下标去删除元素 //list.RemoveRange(0, 3);//根据下标去删除一定范围内的元素 //list.Reverse();//反转 //list.Sort();//升序排列 list.Insert(1, "insert");//在指定的位置,插入一个元素 list.InsertRange(1, list);//在指定的位置,插入一个集合 bool tt= list.Contains(123);//判断集合包含不包含改元素 for(int i=0;i
***集合的长度
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ArrayList长度问题{ class Program { static void Main(string[] args) { System.Collections.ArrayList list = new System.Collections.ArrayList(); list.Add(123); //Count:表示这个集合中实际包含的元素个数 //Capacity:表示这个集合中可以包含的元素个数 Console.WriteLine(list.Count); Console.WriteLine(list.Capacity); //**** //每次集合中实际包含的个数(count)超过了可以包含的元素个数(Capacity)的时候, //集合就会在内存中申请多开辟一倍的空间,来保证集合的长度够用。 Console.ReadKey(); } }}
2.Hashtable 集合 --> 键值对集合
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;using System.Diagnostics;namespace HashTable{ class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add(1,"111"); ht.Add(2, "222"); ht.Add(true, "True"); ht.Add(false, "False"); ht[1] = "123456789"; //在键值对集合中 是根据键去找值得 //Console.WriteLine(ht[true]); foreach (var item in ht.Keys) { Console.WriteLine(ht[item]); } //for (int i = 0; i < ht.Count;i++ ) //{ // Console.WriteLine(ht[i]); //} //键值对里面的键是唯一的,值可以重复! //常用方法:判断键是否唯一 if(!ht.ContainsKey(1)) { ht.Add(1, "123456789"); } else { Console.WriteLine("已包含"); } //常用方法 ht.Clear(); ht.Remove(2); //**** //问:for/foreach 在循环次数很多很多的情况下,谁的效率高 //foreach循环效率要高很多! //****测试程序运行时间 /* Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 10000000; i++) { } sw.Stop(); Console.WriteLine(sw.Elapsed); * */ Console.ReadKey(); } }}
**C# var 关键字
地址:https://www.cnblogs.com/ggll611928/p/5991401.html
3.简体繁体子转换 用Hasgtable集合
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;namespace ConsoleApplication2{ class Program { private const string jian = "常数声明可以声明多个常数"; private const string fan = "常2聲明可以聲明多個常數"; static void Main(string[] args) { Hashtable ht = new Hashtable(); //Console.WriteLine(jian[1]); for (int i = 0; i < jian.Length; i++) { if(!ht.ContainsKey(jian[i])) { ht.Add(jian[i], fan[i]); } } string input = Console.ReadLine(); for (int i = 0; i < input.Length; i++) { if(ht.ContainsKey(input[i])) { Console.Write(ht[input[i]]); } else { Console.Write(input[i]); } } Console.ReadKey(); } }}