Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double[,] type, how to get the # of rows? [duplicate]

Tags:

arrays

c#

I seem to be getting an odd value.

How do I get the number of rows in my array:

double[,] lookup = { {1,2,3}, {4,5,6} };

The output should be 2.

like image 839
Blankman Avatar asked Mar 24 '09 15:03

Blankman


People also ask

How do you use double data type?

This data type is widely used by programmers and is used to store floating-point numbers. All real numbers are floating-point values. A variable can be declared as double by adding the double keyword as a prefix to it. You majorly used this data type where the decimal digits are 14 or 15 digits.

What is the double data type?

double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice.

What is double [] in Java?

Java double is used to represent floating-point numbers. It uses 64 bits to store a variable value and has a range greater than float type.

What is double * in C++?

Definition of C++ Double Data Type. C++ double is a versatile data type that is used internally for the compiler to define and hold any numerically valued data type especially any decimal oriented value.


2 Answers

lookup has two dimensions, this is how you can read them

double[,] lookup = { {1,2,3}, {4,5,6} };

int rows = lookup.GetLength(0); // 2
int cols = lookup.GetLength(1); // 3    
int cells = lookup.Length;      // 6 = 2*3

The concept of rows and cols is just tradition, you might just as well call the first dimension the columns.

Also see this question

like image 168
Henk Holterman Avatar answered Sep 20 '22 18:09

Henk Holterman


You want the rank property on the array...

double[,] lookup = { { 1, 2, 3 }, { 4, 5, 6 } };
Console.WriteLine(lookup.Rank);

This will provide you with the number of dimensions.

Edit:
This will only provide you with the number of dimensions for the array as opposed to the number of primary elements or "rows" see @Henk Holterman's answer for a working solution.

like image 44
Quintin Robinson Avatar answered Sep 19 '22 18:09

Quintin Robinson