Arrays
string[] s=new string[2]; //赋值 s[0]="a"; s[1]="b"; //修改 s[1]="a1";
|
Collections
ArrayList
对数组的封装,允许不同数据类型,装箱操作影响性能
ArrayList list1 = new ArrayList(); //新增数据 list1.Add("cde"); list1.Add(5678); //修改数据 list[2] = 34; //移除数据 list.RemoveAt(0); //插入数据 list.Insert(0, "qwe");
|
List
声明时必须指定类型,所以没有了装箱拆箱操作,这个性能较好。尽量用这个
List<string> list = new List<string>(); //新增数据 list.Add(“abc”); //修改数据 list[0] = “def”; //移除数据 list.RemoveAt(0); // 复制数据,不是引用 name_list2 = new List<string>(name_list1); // 排序 list = list.OrderBy(it => it.percent).ToList();
scores = scores.OrderByDescending(x=>x).ToList();
List<string> scores = new List<string>(); scores = scores.OrderBy(q => q).ToList();
|
LinkedList
用双链表实现的List,特点是插入删除快,查找慢
LinkedList<string> list = new LinkedList<string>(); list.AddFirst("Data Value 1"); list.AddLast("Data Value 6");
|
Dictionary
底层使用 Hash 实现
Dictionary<int, string> test = new Dictionary<int, string> { }; test.Add(0,"000"); test.Add(4, "444"); test.Add(2, "222"); test.Add(6, "666");
Dictionary<int, string> dic1Asc = test.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
Console.WriteLine("小到大排序"); foreach(KeyValuePair<int,string> k in dic1Asc){ Console.WriteLine("key:" +k.Key +" value:" + k.Value); }
Console.WriteLine("大到小排序"); Dictionary<int, string> dic1desc = test.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
foreach (KeyValuePair<int, string> k in dic1desc) { Console.WriteLine("key:" + k.Key + " value:" + k.Value); }
|