大雜燴
一、數組初始化
1.一維數組
String[] str = new String[3] { "1","2","3"};
String[] str2 = { "1","2","3"};
2.二維數組
String[,] str = { { "1","2"}, {"3","4" }, {"5","6" } };
String[,] str2 = new String[3, 2] { { "1", "2" }, { "3", "4" }, { "5", "6" } };
二、數組排序
int[] str = {1,20,3,56,85,66,99,9556,464,48,1,115,1553 };
Array.Sort(str);//升序
Array.Reverse(str);//降序
三、數組合並
Array.Copy(str,str2,10);//從索引值0開始,取10個長度放入
Array.Copy(str1,0,str2,10,10);//str1從0開始,str2從10開始,str1向str2復制10個元素
四、ArrayList
引入命名空間:using System.Collections;
添加元素
string[] str = { "張三","李四","王五","趙六"};
ArrayList arrayList = new ArrayList();
// arrayList.AddRange(str);//把元素逐一添加進去
arrayList.Add(str);//當對象添加進去
移除元素
arrayList.Remove("李四");
arrayList.RemoveAt(1);
arrayList.RemoveRange(0, 2);
arrayList.Clear();
查找元素
arrayList.IndexOf("王五");
//BinarySearch查找之前要排序
/*二分查找要求字典在順序表中按關鍵碼排序,即待查表為有序表。*/
arrayList.Sort();
arrayList.BinarySearch()
五、List
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /* 非泛型集合 ArrayList Hashtable 泛型集合 List<T> Dictionary<Tkey,Tvalue> */ namespace 裝箱和拆箱 { class Program { static void Main(string[] args) { List<int> list = new List<int>(); list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 }); foreach(var item in list) { Console.WriteLine(item); } list.RemoveAll(n=>n>2);//篩選移除 foreach(var item in list) { Console.WriteLine(item); } Console.ReadKey(); } } }