The number of rows in a two-dimensional array can be obtained using the length property, but because of the support for irregular arrays in C #, the number of columns in each row in a two-dimensional array may not be the same, how can I get the number of columns in each dimension of a two-dimensional array? The answer is also the length property, because each dimension of a two-dimensional array can be considered a one-dimensional array, and the length of a one-dimensional array can be obtained using the length property. For example, the following code defines an irregular two-dimensional array and outputs the contents of a two-dimensional array by traversing its number of rows and columns, with the following code:
01 staticvoid Main(string[] args)02 {03 int[][] arr = newint[3][];// 创建二维数组,指定行数,不指定列数04 arr[0] = newint[5];// 第一行分配5个元素05 arr[1] = newint[3];// 第二行分配3个元素06 arr[2] = newint[4];// 第三行分配4个元素07 for(int i=0;i<arr.Length;i++)//遍历行数08 {09 for(int j = 0; j <arr[i].Length; j++)//遍历列数10 {11 Console.Write(arr[i][j]);//输出遍历到的元素12 }13 Console.WriteLine();//换行输出14 }15 Console.ReadLine();16 }
How to get the number of columns in a two-dimensional array