C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別
多維數(shù)組的聲明
在聲明時(shí),必須指定數(shù)組的長(zhǎng)度,格式為 type [lenght ,lenght ,lengh, ... ]
int [,] test1 = new int [3,3];
或聲明時(shí)即賦值,由系統(tǒng)推斷長(zhǎng)度
int [,] test1 = {
{1,2,3},
{1,2,3},
{1,2,3},
};交錯(cuò)數(shù)組的聲明
聲明時(shí),至少需要指定第一維的長(zhǎng)度,格式為 type [ ] [ ] [ ] ...
int [][] test1 = new int[5][];
int [][] test1 = new int[][]; //注意,此的聲明方式是錯(cuò)的
或者聲明時(shí)即賦值,由系統(tǒng)推斷長(zhǎng)度
int [][] test1 = {
new int[] {1,2,3,4},
new int[] {1,2,3},
new int[] {1,2}
};多維數(shù)組與交錯(cuò)數(shù)組 二者的相同、區(qū)別
兩者聲明時(shí),都必須指定長(zhǎng)度,多維數(shù)組必須指定每一維的長(zhǎng)度,而交錯(cuò)數(shù)組需要至少需要指定第一維的長(zhǎng)度。
多維數(shù)組聲明時(shí),符號(hào)是這樣的 [ , , , , ],逗號(hào)在 方括號(hào) [ ] 中,每一維長(zhǎng)度用逗號(hào)分隔。而交錯(cuò)數(shù)組每一維獨(dú)立在 [ ]中
當(dāng)你想指定數(shù)組長(zhǎng)度時(shí),只能在等號(hào)右側(cè)指定,int [,] test1 = new int [3,3] 是正確的 ;int [6,4] test1 = new int [6,4] 是錯(cuò)誤的;
下面以代碼形式說(shuō)明
大小不一致的多維數(shù)組會(huì)發(fā)生錯(cuò)誤
int [,] test1 = {
{1,2,3,4},
{1,2,3},
{1,2}
}; //這樣是錯(cuò)的,長(zhǎng)度必須一致int [,] test1 = new int [4,5] {
{1,2,3,4,5},
{1,2,3},
{1,2,3}
}; //這樣也是錯(cuò)誤的,長(zhǎng)度必須一致,必須為每一個(gè)位置賦值這一點(diǎn)C#與C語(yǔ)言有所區(qū)別,C語(yǔ)言可以不全賦值,沒(méi)有賦值的位置系統(tǒng)默認(rèn)為0。
下面的方法是正確的
int [,] test1 = {
{1,2,3},
{1,2,3},
{1,2,3}
};初始化交錯(cuò)數(shù)組
上面已經(jīng)說(shuō)了聲明一個(gè)交錯(cuò)數(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[],這正是交錯(cuò)數(shù)組的特性。
交錯(cuò)數(shù)組是由數(shù)組構(gòu)成的數(shù)組,交錯(cuò)數(shù)組要求為內(nèi)部的每個(gè)數(shù)組都創(chuàng)建實(shí)例。
即交錯(cuò)數(shù)組的每一維都是一個(gè)實(shí)例,每一個(gè)實(shí)例為一個(gè)數(shù)組。
數(shù)組的長(zhǎng)度是固定的
無(wú)論多維數(shù)組還是交錯(cuò)數(shù)組,長(zhǎng)度都是固定的,不能隨意改變。
獲取數(shù)組的長(zhǎng)度
使用 對(duì)象.Length 獲取數(shù)組的長(zhǎng)度,需要注意的是,多維數(shù)組的長(zhǎng)度是每一維相乘,即元素總個(gè)數(shù)。
int [,] test1 = {
{1,2,3},
{1,2,3},
{1,2,3}
};
Console.WriteLine(test1.Length);
輸出為 9而交錯(cuò)數(shù)組的長(zhǎng)度則是“內(nèi)部組成的數(shù)組的個(gè)數(shù)”,例如
int [][] test1 = {
new int[] {1,2,3},
new int[] {1,2,3},
new int[] {1,2,3},
};
Console.WriteLine(test1.Length);
輸出為 3方法
多維數(shù)組、交錯(cuò)數(shù)組的方法無(wú)差別,都具有Sort()、Clear()等方法,這里不再贅述,關(guān)于數(shù)組的高級(jí)用法,請(qǐng)查閱
http://www.dbjr.com.cn/Special/265.htm
下列為System.Array類的屬性

由于系統(tǒng)提供的方法比較多,有興趣請(qǐng)查閱
https://docs.microsoft.com/zh-cn/dotnet/api/system.array?view=netframework-4.7.2
使用數(shù)組初始化類型
在C#中有 lambda、匿名類等等,C# 5.0/6.0 后,給聲明類、聲明類型類型、賦值等有了很方便的操作方法。下面舉例測(cè)試。
例子1
使用數(shù)組對(duì)集合、集合泛型等初始化
聲明一個(gè) List 泛型集合
using System.Collections.Generic; //頭部引入
//main中的代碼
static void Main(string[] args)
{
List<string> list = new List<string>();
Console.ReadKey();
}那么,給集合 list 增加一個(gè)項(xiàng),用 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ù)組” 來(lái)添加新項(xiàng)
List<string> list = new List<string>(){"a","b","c","d","e","f"};
List<string> list = new List<string>{"a","b","c","d","e","f"};
//以上兩種方法都可以,注意后面有沒(méi)有 ()例子2
上面的例子利用數(shù)組直接在集合聲明時(shí)初始化,但是不能很好的聲明“騷操作”。
試試交錯(cuò)數(shù)組
1,在 program類 所在的命名空間中寫(xiě)一個(gè)類
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等類型,所以在對(duì)類(引用類型)使用數(shù)組、集合等形式時(shí),可以用 “交錯(cuò)數(shù)組” 來(lái)理解。
以上所述是小編給大家介紹的C#中多維數(shù)組[,]和交錯(cuò)數(shù)組[][]的區(qū)別,希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#?wpf?無(wú)邊框窗口添加陰影效果的實(shí)現(xiàn)
在本篇內(nèi)容中小編給大家整理了一篇關(guān)于C#?wpf?無(wú)邊框窗口添加陰影效果的具體方法內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下2022-11-11
c#中Invoke與BeginInvoke的用法及說(shuō)明
這篇文章主要介紹了c#中Invoke與BeginInvoke的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來(lái)比較麻煩。今天偶然得之“Aspose.Words.dll”可以實(shí)現(xiàn)2013-08-08

