詳解C#編程中一維數(shù)組與多維數(shù)組的使用
一維數(shù)組
可按下面的示例所示聲明五個整數(shù)的一維數(shù)組。
int[] array = new int[5];
此數(shù)組包含從 array[0] 到 array[4] 的元素。 new 運(yùn)算符用于創(chuàng)建數(shù)組并將數(shù)組元素初始化為它們的默認(rèn)值。在此例中,所有數(shù)組元素都初始化為零。
可以用相同的方式聲明存儲字符串元素的數(shù)組。例如:
string[] stringArray = new string[6];
數(shù)組初始化
可以在聲明數(shù)組時將其初始化,在這種情況下不需要級別說明符,因為級別說明符已經(jīng)由初始化列表中的元素數(shù)提供。例如:
int[] array1 = new int[] { 1, 3, 5, 7, 9 };
可以用相同的方式初始化字符串?dāng)?shù)組。下面聲明一個字符串?dāng)?shù)組,其中每個數(shù)組元素用每天的名稱初始化:
string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
如果在聲明數(shù)組時將其初始化,則可以使用下列快捷方式:
int[] array2 = { 1, 3, 5, 7, 9 }; string[] weekDays2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
可以聲明一個數(shù)組變量但不將其初始化,但在將數(shù)組分配給此變量時必須使用 new 運(yùn)算符。例如:
int[] array3; array3 = new int[] { 1, 3, 5, 7, 9 }; // OK //array3 = {1, 3, 5, 7, 9}; // Error
值類型數(shù)組和引用類型數(shù)組
請看下列數(shù)組聲明:
SomeType[] array4 = new SomeType[10];
該語句的結(jié)果取決于 SomeType 是值類型還是引用類型。如果為值類型,則語句將創(chuàng)建一個有 10 個元素的數(shù)組,每個元素都有 SomeType 類型。如果 SomeType 是引用類型,則該語句將創(chuàng)建一個由 10 個元素組成的數(shù)組,其中每個元素都初始化為空引用。
多維數(shù)組
數(shù)組可以具有多個維度。例如,下列聲明創(chuàng)建一個四行兩列的二維數(shù)組。
int[,] array = new int[4, 2];
下列聲明創(chuàng)建一個三維(4、2 和 3)數(shù)組。
int[, ,] array1 = new int[4, 2, 3];
數(shù)組初始化
可以在聲明數(shù)組時將其初始化,如下例所示。
// Two-dimensional array. int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // The same array with dimensions specified. int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // A similar array with string elements. string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" }, { "five", "six" } }; // Three-dimensional array. int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; // The same array with dimensions specified. int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; // Accessing array elements. System.Console.WriteLine(array2D[0, 0]); System.Console.WriteLine(array2D[0, 1]); System.Console.WriteLine(array2D[1, 0]); System.Console.WriteLine(array2D[1, 1]); System.Console.WriteLine(array2D[3, 0]); System.Console.WriteLine(array2Db[1, 0]); System.Console.WriteLine(array3Da[1, 0, 1]); System.Console.WriteLine(array3D[1, 1, 2]); // Getting the total count of elements or the length of a given dimension. var allLength = array3D.Length; var total = 1; for (int i = 0; i < array3D.Rank; i++) { total *= array3D.GetLength(i); } System.Console.WriteLine("{0} equals {1}", allLength, total);
輸出:
1 2 3 4 7 three 8 12 12 equals 12
也可以初始化數(shù)組但不指定級別。
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
int[,] array5; array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK //array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error
以下示例將值分配給特定的數(shù)組元素。
array5[2, 1] = 25;
同樣,下面的示例獲取特定數(shù)組元素的值,并將它賦給變量elementValue。
int elementValue = array5[2, 1];
以下代碼示例將數(shù)組元素初始化為默認(rèn)值(交錯數(shù)組除外):
int[,] array6 = new int[10, 10];
相關(guān)文章
C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法
本文主要介紹了C#?wpf?Bitmap轉(zhuǎn)換成WriteableBitmap的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08C#打包應(yīng)用程序,與.NETFramework介紹
C#打包應(yīng)用程序,與.NETFramework介紹,需要的朋友可以參考一下2013-05-05c#?理解csredis庫實現(xiàn)分布式鎖的詳細(xì)流程
這篇文章主要介紹了c#?理解csredis實現(xiàn)分布式鎖,該庫本身已經(jīng)足夠完善,這里我畫蛇添足一下,為了方便自己的使用,本文通過實例代碼給大家詳細(xì)介紹,需要的朋友可以參考下2022-02-02c# 重載WndProc,實現(xiàn)重寫“最小化”的實現(xiàn)方法
在做“亦歌桌面版”的時候,發(fā)現(xiàn)當(dāng)打開歌詞狀態(tài)下,用最小化隱藏窗體到托盤的話(如下code #1),在調(diào)出發(fā)現(xiàn)歌詞縮小了(雖然顯現(xiàn)的窗體大小跟剛才一樣),從這點看調(diào)用該方法其實窗體大小是改變了的(這個過程只是不可視而已)。2009-02-02