欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

詳解C#編程中一維數(shù)組與多維數(shù)組的使用

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

一維數(shù)組
可按下面的示例所示聲明五個(gè)整數(shù)的一維數(shù)組。

int[] array = new int[5];

此數(shù)組包含從 array[0] 到 array[4] 的元素。 new 運(yùn)算符用于創(chuàng)建數(shù)組并將數(shù)組元素初始化為它們的默認(rèn)值。在此例中,所有數(shù)組元素都初始化為零。
可以用相同的方式聲明存儲(chǔ)字符串元素的數(shù)組。例如:

string[] stringArray = new string[6];

數(shù)組初始化
可以在聲明數(shù)組時(shí)將其初始化,在這種情況下不需要級(jí)別說(shuō)明符,因?yàn)榧?jí)別說(shuō)明符已經(jīng)由初始化列表中的元素?cái)?shù)提供。例如:

int[] array1 = new int[] { 1, 3, 5, 7, 9 };

可以用相同的方式初始化字符串?dāng)?shù)組。下面聲明一個(gè)字符串?dāng)?shù)組,其中每個(gè)數(shù)組元素用每天的名稱初始化:

string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

如果在聲明數(shù)組時(shí)將其初始化,則可以使用下列快捷方式:

int[] array2 = { 1, 3, 5, 7, 9 };
string[] weekDays2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

可以聲明一個(gè)數(shù)組變量但不將其初始化,但在將數(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ù)組
請(qǐng)看下列數(shù)組聲明:

SomeType[] array4 = new SomeType[10];

該語(yǔ)句的結(jié)果取決于 SomeType 是值類型還是引用類型。如果為值類型,則語(yǔ)句將創(chuàng)建一個(gè)有 10 個(gè)元素的數(shù)組,每個(gè)元素都有 SomeType 類型。如果 SomeType 是引用類型,則該語(yǔ)句將創(chuàng)建一個(gè)由 10 個(gè)元素組成的數(shù)組,其中每個(gè)元素都初始化為空引用。


多維數(shù)組
數(shù)組可以具有多個(gè)維度。例如,下列聲明創(chuàng)建一個(gè)四行兩列的二維數(shù)組。

int[,] array = new int[4, 2];

下列聲明創(chuàng)建一個(gè)三維(4、2 和 3)數(shù)組。

int[, ,] array1 = new int[4, 2, 3];

數(shù)組初始化
可以在聲明數(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ù)組但不指定級(jí)別。

int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

如果選擇聲明一個(gè)數(shù)組變量但不將其初始化,必須使用 new 運(yùn)算符將一個(gè)數(shù)組分配給此變量。以下示例顯示 new 的用法。
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)值(交錯(cuò)數(shù)組除外):

int[,] array6 = new int[10, 10];

上一篇:Dynamic和Var的區(qū)別及dynamic使用詳解

欄    目:C#教程

下一篇:基于C#生成條形碼操作知識(shí)匯總附源碼下載

本文標(biāo)題:詳解C#編程中一維數(shù)組與多維數(shù)組的使用

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6756.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有