0
Visualizing 3D array C#
Im having trouble visualizing 3d arrays and wonder how Im supposed to visualize for this: int[ , ] someNums = { {2, 3}, {5, 6}, {4, 6} }; can someone please explain? also the sololearn lesson also says it has 2 rows and 3 columns and it just makes no sense to me right now how that is
8 Respostas
+ 11
Let's start with a integer:
int num = 3;
This is just a single plain value and nothing more.
For 1D integer array, we would have a collection of similar items, in this case, a bunch of integers.
int[] numArr = { 1, 2, 3 };
Notice we can either read the number forward or backward in horizontal way (single direction). That's why we only need 1 for loop to iterate 1D array.
For 2D array, just imagine for each of the value 1, 2, 3 above, we replace it with arrays instead. Therefore essentially it's an array of array.
int [,] numArr =
{
{ 1, 1, 1 },
{ 2, 2, 2 },
{ 3, 3, 3 }
};
We can traverse the 2D array in both ways, either up/down or left/right. (I rearrange it so you can visualize it, you may write it in 1 line too) Therefore we would need 2 for loop (nested) to iterate 2D array.
Same logic applies to any dimension beyond 2D and the process continue. Therefore an 8D array would be an array of array of array of array of... đ
+ 10
The example you've given is a 3 Ă 2 2D array. 3D array would have 3 nested level deep of braces. (i.e. you'll further replace the value with an array above to get 3 leading opening braces) đ
Here's a good read for you:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays
+ 8
Yes you're right! đ
+ 2
I accidentally described a 2d array, because looking at the variable, it's declaring a 2d array.
+ 2
However, a 3d array is simply multiple 2d arrays, similar to a shape of a cube
0
noooo i thought i finally got it...
0
can you explain why the 3D array consists of 3 rows and 2 columns?
0
ah so that wasn't even a 3d array?