c#中的數據類型簡介(數組)
數組定義
- 可以將數組看成相同數據類型的一組或多組數據,包括一維數組,多維數組和交錯數組。
- 數值數組元素的默認值設置為零,而引用元素的默認值設置為 null。
- 交錯數組是指元素為數組的數組,因此,它的元素是引用類型,初始化為 null。
- 數組的索引從零開始:具有 n 個元素的數組的索引是從 0 到 n-1。
數據申明
一維數組的幾種申明和初始化
type[] typeName = new type[n]; //定義數組但是未賦值
type[0] = item1;
type[1] = item2;
type[2] = item3; ......
type[n-1] =itemn;
type[] typeName = new type[n] { item1,item2,item3,......itemn};//系統可以自動推算數組長度
type[] typeName = new type[] { item1,item2,item3,......itemn}; //系統可以自動推算數組長度
type[] typeNmae = {item1,item2,item3,......itemn}; //省略賦值的數據類型,系統可以自動推斷,最簡潔的定義方式
多維數組和交錯數組的申明和初始化與一維數組類似,請看以下示例程序:
class Program { static void Main(string[] args) { int[] intA = new int[5]; intA[0] = 1; intA[1] = 2; intA[2] = 3; intA[3] = 4; intA[4] = 5; int[] intB = new int[] { 1, 2, 3, 4, 5 }; int[] intC = new int[5] { 1, 2, 3, 4, 5 }; int[] intD = { 1, 2, 3, 4, 5 }; //數組的取值,返回索引位3的值,返回的值為3 int item3 = intA[2]; //int[]的抽象基類System.Array,繼承了IEnumerable接口,可以使用foreach遍歷數組成員 foreach (int item in intA) Console.WriteLine("item is:{0}", item); //可以使用for循環訪問數組成員 for (int i = 0; i < intA.Length; i++) Console.WriteLine("each is :{0}", intA[i]); //表明數組成員可以被賦值 bool b1 = intA.IsReadOnly; //返回false bool b2 = intA.IsSynchronized; //返回false,可以異步訪問 //數組一經定義好后,是不能被新增、插入、刪除的 bool b3 = intA.IsFixedSize; //返回true,表示數組是固定長度的 //多維數組申明和初始化 string[,] strA = new string[3,2]{ { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } }; string[,] strB = new string[,] { { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } }; string[,] strC = { { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } }; string[,] strD = new string[3, 2]; strD[0, 0] = "GZ"; strD[0, 1] = "SZ"; //多維數組的取值 string item11 = strA[1, 1]; string itemX = strD[2,1]; //初始化未賦值,其默認值為null //多維數組成員foreach遍歷 foreach (string item in strA) Console.WriteLine("foreach遍歷:{0}",item); //多維度成員的for遍歷,w1和w2用來計算各維度的元素個數,也可用getLength方法獲取 int w1 = strA.GetUpperBound(1)+1; int w2 = strA.GetUpperBound(0)+1; for (int i = 0; i < w2; i++) for (int j = 0; j < w1; j++) Console.WriteLine("for遍歷:{0}",strA[i, j]); //交錯數組,表示成員為數組的數組 int[][] arry = new int[2][]; arry[0] = new int[3] { 6, 7, 8 }; arry[1] = new int[4] { 9, 4, 5, 6 }; foreach (var item in arry) foreach (int element in item) Console.WriteLine(element); } }
數組抽象基類System.Array
System.Array是具體數組的抽象類,具體數組繼承自System.Array類,通過Array可以創建一維,多維數組,並遍歷數組。請看如下的程序示例:
static void Main(string[] args) { //使用System.Array靜態方法CreateInstance創建數組實例 Array strArray = Array.CreateInstance(typeof(string), 4); strArray.SetValue("beijing", 0); strArray.SetValue("shanghai", 1); strArray.SetValue("tianjin", 2); strArray.SetValue("chongqin", 3); //上面的寫法等價於 Array strArray1 = new string[4]{"beijing","shanghai","tianjin","chongqin"}; //foreach 遍歷數組 foreach (var item in strArray) Console.WriteLine("foreach一維數組遍歷list:{0}",item); //for 遍歷數組 for (int i = 0; i < strArray.Length; i++) Console.WriteLine("for一維數組遍歷list:{0}",strArray.GetValue(i)); //反轉一維數組strArray Array.Reverse(strArray); //Array 創建多維數組 Array strArray2 = Array.CreateInstance(typeof(int), new int[] { 3, 2 }); strArray2.SetValue(10,new int[]{0,0}); strArray2.SetValue(11, new int[] { 0, 1 }); strArray2.SetValue(12, new int[] { 1, 0 }); strArray2.SetValue(13, new int[] { 1, 1 }); strArray2.SetValue(14, new int[] { 2, 0 }); strArray2.SetValue(15, new int[] { 2, 1 }); //上面的多維數組定義等價於 int[,] strArray3 = new int[3,2] { { 10, 11 }, { 12, 13 }, { 14, 15 } }; foreach (var item in strArray2) Console.WriteLine("foreach多維數組遍歷:{0}",item); //0維度的長度為3,其上限為2,其下限為0,運行結果顯示OK,OK if (strArray2.GetLength(0) == strArray2.GetUpperBound(0) - strArray2.GetLowerBound(0) + 1) Console.WriteLine("OK,OK"); }