How to get the number of columns in a two-dimensional array

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.