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.
4 Antworten
+ 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.
+ 1
Yes.
The first array is pointing to 4 other arrays.
The 4 other arrays are pointing to their corresponding values.
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?
0
Oops my mistake, the brackets should have two spaces, like this: [ , ].