2012-02-18 48 views

回答

7

這個問題真的取決於你在想什麼類型的二維數組,以及你的行的維數。

正確2D陣列

// Init 
var arr = new int[5,10]; 

// Counts 
arr.GetLength(0) // Length of first dimension: 5 
arr.GetLength(1) // Length of second dimension: 10 

鐵血 「2D」 陣列

// Init 
var arr = new int[3][]; 
// Initially arr[0],arr[1],arr[2] is null, so we have to intialize them: 
arr[0] = new int[5]; 
arr[1] = new int[4]; 
arr[2] = new int[2]; 

// Counts 
arr.Length // Length of first dimension 
// In this case the "second dimension" (if you can call it that) is of variable size 
arr[0].Length // Length: 5 
arr[1].Length // Length: 4 
arr[2].Length // Length: 2 
+0

謝謝,2d數組在C#中有很大不同# – CodeKingPlusPlus 2012-02-18 17:54:20

+0

@CodeKingPlusPlus不客氣:-) – 2012-02-18 17:55:06

0

這也將工作,

string[,] a = new string[,] 
{ 
    {"ant", "aunt"}, 
    {"Sam", "Samantha"}, 
    {"clozapine", "quetiapine"}, 
    {"flomax", "volmax"}, 
    {"toradol", "tramadol"} 
}; 

// a[1,1] is equal to Samantha