0

Initializing Multidimensional Arrays

Can anyone thoroughly explain to me how this code produces three rows and two columns: int[ , ] someNums = { {2, 3}, {5, 6}, {4, 6} };. Thanks in advance.

2nd Jun 2017, 1:23 AM
Unknown Unknown
Unknown Unknown - avatar
4 Respostas
+ 1
The first { // which surrounds everything else } Is the first array. Or, as an analogy the 'rows'. The rest are what arrays the first array points to. Or, as an analogy, the 'columns'. So, { } is an array pointing to these arrays: {2, 3}, {5, 6} , {4, 6}. The first array is pointing to 3 arrays. So, the length of that is 3. Or, there's 3 rows. Each array that is pointed to is of size 2. So, the length of each of those arrays is 2. Or, each row has 2 columns.
2nd Jun 2017, 1:43 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
Yes. The first array is pointing to 4 other arrays. The 4 other arrays are pointing to their corresponding values.
2nd Jun 2017, 2:00 AM
Rrestoring faith
Rrestoring faith - avatar
0
Oh I see. So if my code was like this: int[ , , ] myArray = { {5, 8}, {4,5}, {6,2}, {8,7} }; there would be 4 rows because there are four arrays, and two columns, because there are two numbers in each array. Correct?
2nd Jun 2017, 1:50 AM
Unknown Unknown
Unknown Unknown - avatar
0
Oops my mistake, the brackets should have two spaces, like this: [ , ].
2nd Jun 2017, 1:51 AM
Unknown Unknown
Unknown Unknown - avatar