多維數組的聲明api
在聲明時,必須指定數組的長度,格式爲 type [lenght ,lenght ,lengh, ... ]數組
int [,] test1 = new int [3,3];
或聲明時即賦值,由系統推斷長度測試
int [,] test1 = { {1,2,3}, {1,2,3}, {1,2,3}, };
交錯數組的聲明spa
聲明時,至少須要指定第一維的長度,格式爲 type [ ] [ ] [ ] ....net
int [][] test1 = new int[5][];
int [][] test1 = new int[][]; //注意,此的聲明方式是錯的
或者聲明時即賦值,由系統推斷長度 code
int [][] test1 = { new int[] {1,2,3,4}, new int[] {1,2,3}, new int[] {1,2} };
多維數組與交錯數組 兩者的相同、區別htm
二者聲明時,都必須指定長度,多維數組必須指定每一維的長度,而交錯數組須要至少須要指定第一維的長度。對象
多維數組聲明時,符號是這樣的 [ , , , , ],逗號在 方括號 [ ] 中,每一維長度用逗號分隔。而交錯數組每一維獨立在 [ ]中blog
當你想指定數組長度時,只能在等號右側指定,int [,] test1 = new int [3,3] 是正確的 ;int [6,4] test1 = new int [6,4] 是錯誤的;內存
下面以代碼形式說明
大小不一致的多維數組會發生錯誤
int [,] test1 = { {1,2,3,4}, {1,2,3}, {1,2} }; //這樣是錯的,長度必須一致
int [,] test1 = new int [4,5] { {1,2,3,4,5}, {1,2,3}, {1,2,3} }; //這樣也是錯誤的,長度必須一致,必須爲每個位置賦值
這一點C#與C語言有所區別,C語言能夠不全賦值,沒有賦值的位置系統默認爲0。
下面的方法是正確的
int [,] test1 = { {1,2,3}, {1,2,3}, {1,2,3} };
初始化交錯數組
上面已經說了聲明一個交錯數組的方法
int [][] test1 = { new int[] {1,2,3,4}, //new int[4] {1,2,3,4} new int[] {1,2,3}, //new int[3] {1,2,3} new int[] {1,2} };
注意,在裏面有 new int[],這正是交錯數組的特性。交錯數組是由數組構成的數組,交錯數組要求爲內部的每一個數組都建立實例。
即交錯數組的每一維都是一個實例,每個實例爲一個數組。
數組的長度是固定的
不管多維數組仍是交錯數組,長度都是固定的,不能隨意改變。
獲取數組的長度
使用 對象.Length 獲取數組的長度,須要注意的是,多維數組的長度是每一維相乘,即元素總個數。
int [,] test1 = { {1,2,3}, {1,2,3}, {1,2,3} }; Console.WriteLine(test1.Length); 輸出爲 9
而交錯數組的長度則是「內部組成的數組的個數」,例如
int [][] test1 = { new int[] {1,2,3}, new int[] {1,2,3}, new int[] {1,2,3}, }; Console.WriteLine(test1.Length);
輸出爲 3
方法
多維數組、交錯數組的方法無差異,都具備Sort()、Clear()等方法,這裏再也不贅述,關於數組的高級用法,請查閱
https://www.jb51.net/Special/265.htm
下列爲System.Array類的屬性
因爲系統提供的方法比較多,有興趣請查閱
https://docs.microsoft.com/zh-cn/dotnet/api/system.array?view=netframework-4.7.2
在C#中有 lambda、匿名類等等,C# 5.0/6.0 後,給聲明類、聲明類型類型、賦值等有了很方便的操做方法。下面舉例測試。
例子1
使用數組對集合、集合泛型等初始化
聲明一個 List 泛型集合
using System.Collections.Generic; //頭部引入 //main中的代碼 static void Main(string[] args) { List<string> list = new List<string>(); Console.ReadKey(); }
那麼,給集合 list 增長一個項,用 Add() 方法
static void Main(string[] args) { List<string> list = new List<string>(); //增長 list.Add("a"); list.Add("b"); list.Add("c"); list.Add("d"); list.Add("e"); list.Add("f"); list.Add("g"); Console.ReadKey(); }
利用 「數組」 來添加新項
List<string> list = new List<string>(){"a","b","c","d","e","f"}; List<string> list = new List<string>{"a","b","c","d","e","f"}; //以上兩種方法均可以,注意後面有沒有 ()
例子2
上面的例子利用數組直接在集合聲明時初始化,可是不能很好的聲明「騷操做」。
試試交錯數組
1,在 program 類 所在的命名空間中寫一個類
public class Test { public int x; public int y; public void What() { Console.WriteLine(x + y); } }
2,在 Main 方法中
static void Main(string[] args) { List<Test> list = new List<Test>() { new Test{x=1,y=6}, new Test{x=8,y=6}, new Test{x=4,y=8}, new Test{x=5,y=7}, new Test{x=3,y=3}, new Test{x=6,y=6}, new Test{x=9,y=666}, new Test{x=7,y=6}, }; Console.ReadKey(); }
完整代碼以下
public class Test { public int x; public int y; public void What() { Console.WriteLine(x + y); } } class Program { static void Main(string[] args) { List<Test> list = new List<Test>() { new Test{x=1,y=6}, new Test{x=8,y=6}, new Test{x=4,y=8}, new Test{x=5,y=7}, new Test{x=3,y=3}, new Test{x=6,y=6}, new Test{x=9,y=666}, new Test{x=7,y=6}, }; Console.ReadKey(); } }
因爲類引用類型,它的內存是引用地址,不像 int、char等類型,因此在對類(引用類型)使用數組、集合等形式時,能夠用 「交錯數組」 來理解。