多元數組的聲明
在聲明時,必須指定數組的長度,格式為 type [lenght ,lenght ,lengh, ... ]
int [,] test1 = new int [3,3];
或聲明時即指派,由系統推斷長度
int [,] test1 = {
{1,2,3},
{1,2,3},
{1,2,3},
};
交錯數組的聲明
聲明時,至少需要指定第一維的長度,格式為 type [ ] [ ] [ ] ...
int [][] test1 = new int[5][];
int [][] test1 = new int[][]; //注意,此的聲明方式是錯的
或者聲明時即指派,由系統推斷長度
int [][] test1 = {
new int[] {1,2,3,4},
new int[] {1,2,3},
new int[] {1,2}
};
多元數組與交錯數組 二者的相同、差別
兩者聲明時,都必須指定長度,多元數組必須指定每一維的長度,而交錯數組需要至少需要指定第一維的長度。
多元數組聲明時,符号是這樣的 [ , , , , ],逗号在 方括号 [ ] 中,每一維長度用逗号分隔。而交錯數組每一維獨立在 [ ]中
當你想指定數組長度時,隻能在等号右側指定,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()等方法,這裡不再贅述,關于數組的進階用法,請查閱
下列為System.Array類的屬性
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iYiRDZyUGMhZTMyEGNihTY4ImM1YjNzUWZ5czYiBDZm9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
由于系統提供的方法比較多,有興趣請查閱
使用數組初始化類型
在C#中有 lambda、匿名類等等,C# 5.0/6.0 後,給聲明類、聲明類型類型、指派等有了很友善的操作方法。下面舉例測試。
例子1
使用數組對集合、集合泛型等初始化
聲明一個List 泛型集合
using System.Collections.Generic; //頭部引入//main中的代碼
static void Main(string[] args)
{
List list = new List();
Console.ReadKey();
}
那麼,給集合list 增加一個項,用 Add() 方法
static void Main(string[] args)
{
List list = new List();//增加
list.Add("a");
list.Add("b");
list.Add("c");
list.Add("d");
list.Add("e");
list.Add("f");
list.Add("g");
Console.ReadKey();
}
利用“數組” 來添加新項
List list = new List(){"a","b","c","d","e","f"};
List list = new List{"a","b","c","d","e","f"};//以上兩種方法都可以,注意後面有沒有 ()
例子2
上面的例子利用數組直接在集合聲明時初始化,但是不能很好的聲明“騷操作”。
試試交錯數組
1,在 program 類 所在的命名空間中寫一個類
public classTest
{public intx;public inty;public voidWhat()
{
Console.WriteLine(x+y);
}
}
2,在 Main 方法中
static void Main(string[] args)
{
List list = new List()
{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 classTest
{public intx;public inty;public voidWhat()
{
Console.WriteLine(x+y);
}
}classProgram
{static void Main(string[] args)
{
List list = new List()
{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等類型,是以在對類(引用類型)使用數組、集合等形式時,可以用 “交錯數組” 來了解。