C#中多維數(shù)組[,]和交錯數(shù)組[][]的區(qū)別
多維數(shù)組的聲明
在聲明時,必須指定數(shù)組的長度,格式為 type [lenght ,lenght ,lengh, ... ]
int [,] test1 = new int [3,3];
或聲明時即賦值,由系統(tǒng)推斷長度
int [,] test1 = { {1,2,3}, {1,2,3}, {1,2,3}, };
交錯數(shù)組的聲明
聲明時,至少需要指定第一維的長度,格式為 type [ ] [ ] [ ] ...
int [][] test1 = new int[5][];
int [][] test1 = new int[][]; //注意,此的聲明方式是錯的
或者聲明時即賦值,由系統(tǒng)推斷長度
int [][] test1 = { new int[] {1,2,3,4}, new int[] {1,2,3}, new int[] {1,2} };
多維數(shù)組與交錯數(shù)組 二者的相同、區(qū)別
兩者聲明時,都必須指定長度,多維數(shù)組必須指定每一維的長度,而交錯數(shù)組需要至少需要指定第一維的長度。
多維數(shù)組聲明時,符號是這樣的 [ , , , , ],逗號在 方括號 [ ] 中,每一維長度用逗號分隔。而交錯數(shù)組每一維獨立在 [ ]中
當你想指定數(shù)組長度時,只能在等號右側(cè)指定,int [,] test1 = new int [3,3] 是正確的 ;int [6,4] test1 = new int [6,4] 是錯誤的;
下面以代碼形式說明
大小不一致的多維數(shù)組會發(fā)生錯誤
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語言有所區(qū)別,C語言可以不全賦值,沒有賦值的位置系統(tǒng)默認為0。
下面的方法是正確的
int [,] test1 = { {1,2,3}, {1,2,3}, {1,2,3} };
初始化交錯數(shù)組
上面已經(jīng)說了聲明一個交錯數(shù)組的方法
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[],這正是交錯數(shù)組的特性。
交錯數(shù)組是由數(shù)組構成的數(shù)組,交錯數(shù)組要求為內(nèi)部的每個數(shù)組都創(chuàng)建實例。
即交錯數(shù)組的每一維都是一個實例,每一個實例為一個數(shù)組。
數(shù)組的長度是固定的
無論多維數(shù)組還是交錯數(shù)組,長度都是固定的,不能隨意改變。
獲取數(shù)組的長度
使用 對象.Length 獲取數(shù)組的長度,需要注意的是,多維數(shù)組的長度是每一維相乘,即元素總個數(shù)。
int [,] test1 = { {1,2,3}, {1,2,3}, {1,2,3} }; Console.WriteLine(test1.Length); 輸出為 9
而交錯數(shù)組的長度則是“內(nèi)部組成的數(shù)組的個數(shù)”,例如
int [][] test1 = { new int[] {1,2,3}, new int[] {1,2,3}, new int[] {1,2,3}, }; Console.WriteLine(test1.Length); 輸出為 3
方法
多維數(shù)組、交錯數(shù)組的方法無差別,都具有Sort()、Clear()等方法,這里不再贅述,關于數(shù)組的高級用法,請查閱
http://www.dbjr.com.cn/Special/265.htm
下列為System.Array類的屬性
由于系統(tǒng)提供的方法比較多,有興趣請查閱
https://docs.microsoft.com/zh-cn/dotnet/api/system.array?view=netframework-4.7.2
使用數(shù)組初始化類型
在C#中有 lambda、匿名類等等,C# 5.0/6.0 后,給聲明類、聲明類型類型、賦值等有了很方便的操作方法。下面舉例測試。
例子1
使用數(shù)組對集合、集合泛型等初始化
聲明一個 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(); }
利用 “數(shù)組” 來添加新項
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
上面的例子利用數(shù)組直接在集合聲明時初始化,但是不能很好的聲明“騷操作”。
試試交錯數(shù)組
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(); } }
由于類引用類型,它的內(nèi)存是引用地址,不像 int、char等類型,所以在對類(引用類型)使用數(shù)組、集合等形式時,可以用 “交錯數(shù)組” 來理解。
以上所述是小編給大家介紹的C#中多維數(shù)組[,]和交錯數(shù)組[][]的區(qū)別,希望對大家有所幫助。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來比較麻煩。今天偶然得之“Aspose.Words.dll”可以實現(xiàn)2013-08-08