本文實例講述了C#二維數組基本用法。分享給大家供大家參考,具體如下:
1
2
3
4
5
6
|
//定義數組 string [,] classes = new string [5, 2]; //正確的C#二維數組使用方法 classes[i, 0] = "" ; //錯誤的使用方法 classes[i][0]= "" ; |
據說這種形式的C#二維數組叫做鋸齒數組,
一段例子以供參考:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// 聲明一個鋸齒型數組,該數組有兩個元素 int [][] myArray = new int [2][]; // 其中第一個元素是一個含有五個元素的數組 // 初始化myArray[0] myArray[0] = new int [5] {1,3,5,7,9}; // 其中第二個元素是一個含有4個元素的數組 // 初始化myArray[1] myArray[1] = new int [4] {0, 2, 4, 6}; // 逐一打印出數組的數組中所有的元素 for ( int i=0; i < myArray.Length; i++) { Console.Write( "第({0})個數組: " , i); // 打印一維數組中的元素 for ( int j=0; j < myArray[i].Length; j++) { Console.Write( "{0} " , myArray[i][j]); } Console.WriteLine(); } |
希望本文所述對大家C#程序設計有所幫助。