Gets the number of rows in the two-dimensional array that actually have data

Source: Internet
Author: User

Write the program encountered need to get a two-dimensional array of actual data in the number of rows, see a few blog to get the array row count is actually an array. Rank method. This is the way to get the dimension ah, I posted below the correct method I found, very practical.

/// <summary>///gets the number of rows in the two-dimensional array that actually have data/// </summary>         Public Staticlist<int> Gethasvaluerowindex (string[,] arr) {    varHasvaluerowindex =Newlist<int>();  for(vari =0; I < arr. GetLength (0); i++)    {         for(varj =0; J < arr. GetLength (1); J + +)        {            if(!string.                IsNullOrEmpty (Arr[i, J])) {hasvaluerowindex.add (i);  Break; }        }    }    returnHasvaluerowindex;}//Demostring[,] a =New string[9,3];a[0,0] ="a"; a[2,0] ="b"; a[4,0] ="C"; a[6,0] ="D"; a[8,0] ="e";vararr =Gethasvaluerowindex (a); Console.WriteLine ("HasValue Row Count:"+arr. Count); arr. ForEach (o=Console.WriteLine (o));/*HasValue Row count:5 0 2 4 6 8*/

int[,] array =New int[,] {{1,2,3},{4,5,6},{7,8,9},{Ten, One, A}};//defines a two-dimensional array of 4 rows and 3 columnsintdimension= Array. Rank;//gets the number of dimensions of the array; a value of 2introw = arr. GetLength (0);//returns the length of the first dimension (the so-called "number of Rows") (the number of rows for all data is not the number of rows that actually have data stored)intCol = array. GetLength (1);//Gets the number of elements in the specified dimension, which is the number of columns. (0 is the first dimension, 1 represents the second dimension); The value is 3intCol = array. GetUpperBound (0)+1;//gets the upper limit of the index for the specified dimension, plus a 1 is the total number, which represents the number of rows in a two-dimensional arrayintnum = array. Length;//gets the length of the entire two-dimensional array, that is, the number of all the elements

Gets the number of rows in the two-dimensional array that actually have data

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.