http://www.cnblogs.com/tianhao960/articles/273425.html
*數(shù)組概述******C# 數(shù)組從零開(kāi)始建立索引窝爪,即數(shù)組索引從零開(kāi)始婴噩。C# 中數(shù)組的工作方式與在大多數(shù)其他流行語(yǔ)言中的工作方式類似象对。但還有一些差異應(yīng)引起注意误证。
聲明數(shù)組時(shí),方括號(hào) ([]) 必須跟在類型后面鸯匹,而不是標(biāo)識(shí)符后面灭将。在 C# 中,將方括號(hào)放在標(biāo)識(shí)符后是不合法的語(yǔ)法境输。
int[] table; // not int table[];
另一細(xì)節(jié)是蔗牡,數(shù)組的大小不是其類型的一部分,而在 C 語(yǔ)言中它卻是數(shù)組類型的一部分嗅剖。這使您可以聲明一個(gè)數(shù)組并向它分配 int 對(duì)象的任意數(shù)組辩越,而不管數(shù)組長(zhǎng)度如何。
int[] numbers; // declare numbers as an int array of any size
numbers = new int[10]; // numbers is a 10-element array
numbers = new int[20]; // now it's a 20-element array
聲明數(shù)組******
C# 支持一維數(shù)組信粮、多維數(shù)組(矩形數(shù)組)和數(shù)組的數(shù)組(交錯(cuò)的數(shù)組)黔攒。下面的示例展示如何聲明不同類型的數(shù)組:
一維數(shù)組:
int[] numbers;
多維數(shù)組:
string[,] names;
數(shù)組的數(shù)組(交錯(cuò)的):
byte[][] scores;
聲明數(shù)組(如上所示)并不實(shí)際創(chuàng)建它們。在 C# 中,數(shù)組是對(duì)象(本教程稍后討論)督惰,必須進(jìn)行實(shí)例化不傅。下面的示例展示如何創(chuàng)建數(shù)組:
一維數(shù)組:
int[] numbers = new int[5];
多維數(shù)組:
string[,] names = new string[5,4];
數(shù)組的數(shù)組(交錯(cuò)的):
byte[][] scores = new byte[5][];
for (int x = 0; x < scores.Length; x++)
{
scores[x] = new byte[4];
}
還可以有更大的數(shù)組。例如赏胚,可以有三維的矩形數(shù)組:
int[,,] buttons = new int[4,5,3];
甚至可以將矩形數(shù)組和交錯(cuò)數(shù)組混合使用访娶。例如,下面的代碼聲明了類型為 int 的二維數(shù)組的三維數(shù)組的一維數(shù)組觉阅。
int[][,,][,] numbers;
初始化數(shù)組
C# 通過(guò)將初始值括在大括號(hào) ({}) 內(nèi)為在聲明時(shí)初始化數(shù)組提供了簡(jiǎn)單而直接了當(dāng)?shù)姆椒ㄑ掳獭O旅娴氖纠故境跏蓟煌愋偷臄?shù)組的各種方法。
注意 如果在聲明時(shí)沒(méi)有初始化數(shù)組典勇,則數(shù)組成員將自動(dòng)初始化為該數(shù)組類型的默認(rèn)初始值劫哼。另外,如果將數(shù)組聲明為某類型的字段割笙,則當(dāng)實(shí)例化該類型時(shí)它將被設(shè)置為默認(rèn)值 null权烧。
一維數(shù)組
int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
可省略數(shù)組的大小,如下所示:
int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};
如果提供了初始值設(shè)定項(xiàng)伤溉,則還可以省略 new *運(yùn)算符般码,如下所示:
int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Matt", "Joanne", "Robert"};
多維數(shù)組
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
可省略數(shù)組的大小,如下所示:
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
如果提供了初始值設(shè)定項(xiàng)乱顾,則還可以省略 new 運(yùn)算符侈询,如下所示:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
交錯(cuò)的數(shù)組(數(shù)組的數(shù)組)
可以像下例所示那樣初始化交錯(cuò)的數(shù)組:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
可省略第一個(gè)數(shù)組的大小,如下所示:
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
-或-
int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
請(qǐng)注意糯耍,對(duì)于交錯(cuò)數(shù)組的元素沒(méi)有初始化語(yǔ)法。
訪問(wèn)數(shù)組成員
訪問(wèn)數(shù)組成員可以直接進(jìn)行囊嘉,類似于在 C/C++ 中訪問(wèn)數(shù)組成員温技。例如,下面的代碼創(chuàng)建一個(gè)名為 numbers
的數(shù)組扭粱,然后向該數(shù)組的第五個(gè)元素賦以 5:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4] = 5;
下面的代碼聲明一個(gè)多維數(shù)組舵鳞,并向位于 [1, 1]
的成員賦以 5
:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;
下面聲明一個(gè)一維交錯(cuò)數(shù)組,它包含兩個(gè)元素琢蛤。第一個(gè)元素是兩個(gè)整數(shù)的數(shù)組蜓堕,第二個(gè)元素是三個(gè)整數(shù)的數(shù)組:
int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5}
};
下面的語(yǔ)句向第一個(gè)數(shù)組的第一個(gè)元素賦以 58,向第二個(gè)數(shù)組的第二個(gè)元素賦以 667:
numbers[0][0] = 58;
numbers[1][1] = 667;
數(shù)組是對(duì)象
在 C# 中博其,數(shù)組實(shí)際上是對(duì)象套才。System.Array 是所有數(shù)組類型的抽象基類型∧降可以使用 System.Array 具有的屬性以及其他類成員背伴。這種用法的一個(gè)示例是使用“長(zhǎng)度”(Length) **屬性獲取數(shù)組的長(zhǎng)度。下面的代碼將 numbers
數(shù)組的長(zhǎng)度(為 5
)賦給名為L(zhǎng)engthOfNumbers
的變量:
int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = numbers.Length;
System.Array 類提供許多有用的其他方法/屬性,如用于排序傻寂、搜索和復(fù)制數(shù)組的方法息尺。
對(duì)數(shù)組使用 foreach
C# 還提供 foreach 語(yǔ)句。該語(yǔ)句提供一種簡(jiǎn)單疾掰、明了的方法來(lái)循環(huán)訪問(wèn)數(shù)組的元素搂誉。例如,下面的代碼創(chuàng)建一個(gè)名為 numbers
的數(shù)組静檬,并用 foreach 語(yǔ)句循環(huán)訪問(wèn)該數(shù)組:
int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};
foreach (int i in numbers)
{
System.Console.WriteLine(i);
}
由于有了多維數(shù)組炭懊,可以使用相同方法來(lái)循環(huán)訪問(wèn)元素,例如:
int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
foreach(int i in numbers)
{
Console.Write("{0} ", i);
}
該示例的輸出為:
9 99 3 33 5 55
不過(guò)巴柿,由于有了多維數(shù)組凛虽,使用嵌套 for 循環(huán)將使您可以更好地控制數(shù)組元素。