Having a brain-fart about 2-D arrays

This topic is 2060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all,

It seems that I'm -- embarrassingly -- having a hard-time with 2-D arrays and how to think of them conceptually. I'm currently working in C#, but ultimately, this can be applied to any language out there. If we have an array of foo[1][20], where would it matter that 'X' and 'Y' would be in relation to that array? How would that be handled (I realize that this would be very much compiler/run-time dependent)? Does it matter how I think about this conceptually or am I over-thinking this?

Thanks in advance if you can shed some light on this issue.

Share on other sites

Think of the first number as the row and the second as the column. I don't know what you mean about X and Y in relation to the array, typically x is across though (X is a cross, geddit!!!) and y is up or down.

So a foo[1][20] array would be 1 row and 2 columns. May as well just be a 2D array in that case...

You can think of a 3D array as going into or out of the page. You can't visualise a 4D or higher dimensional array.

Share on other sites

Think of the first number as the row and the second as the column. I don't know what you mean about X and Y in relation to the array, typically x is across though (X is a cross, geddit!!!) and y is up or down.

So a foo[1][20] array would be 1 row and 2 columns. May as well just be a 2D array in that case...

You can think of a 3D array as going into or out of the page. You can't visualise a 4D or higher dimensional array.

Ok, gotcha, much clearer now.

Thanks.

Share on other sites

I think of it how it would be as a one-dimensional array, infact, I don't even use multidimensional arrays anymore (or arrays, if I can avoid it).. I just multiply all my dimensions and allocate one big chunk of memory, which I index as a linear array... In 2D I just imagine each row (eg: where X can lie inside of) being laid out end-to-end in one long line.. In 3D, I think of the same thing, except that once you reach the end of the first 'page' (0,0 to width,height) the beginning of the next Z 'page' is on the end of that.. So, 2D is a 'page' of 'lines', and 3D is just multiple pages. And, just like Paradigm Shifter pointer out, each [] bracket pair indicates another dimension, successively, so that in the case of XY, the first [] is X and the 2nd [] is Y.

Share on other sites

I think of it how it would be as a one-dimensional array, infact, I don't even use multidimensional arrays anymore (or arrays, if I can avoid it).. I just multiply all my dimensions and allocate one big chunk of memory, which I index as a linear array... In 2D I just imagine each row (eg: where X can lie inside of) being laid out end-to-end in one long line.. In 3D, I think of the same thing, except that once you reach the end of the first 'page' (0,0 to width,height) the beginning of the next Z 'page' is on the end of that.. So, 2D is a 'page' of 'lines', and 3D is just multiple pages. And, just like Paradigm Shifter pointer out, each [] bracket pair indicates another dimension, successively, so that in the case of XY, the first [] is X and the 2nd [] is Y.

Well... that's pretty much how it usually works in the memory of a computer . The multiple dimensions just make it easier for you. I realize you can do that, but it's just easier for me to do foo[20,20,20,40], as opposed to foo[320000] . Edited by ysg

Share on other sites

I made some illustrations, and some poorly-worded descriptions in this post that might help.

Share on other sites

Well... that's pretty much how it usually works in the memory of a computer . The multiple dimensions just make it easier for you. I realize you can do that, but it's just easier for me to do foo[20,20,20,40], as opposed to foo[320000] .

Also, easy to understand picture:

Share on other sites

I was going to mention that the code ends up making a single line of memory anyway(but someone already beat me to it).

These days, computers are fast enough that unless you are really needing some massive performance, as in are on a device other than even the lowest end PC, you are better off keeping code readable.  The big picture makes much more difference than a very small detail that makes little difference in speed.  So even if you got a slight speed gain by only using single index arrays, it wouldn't be worth losing the readability.  Also, you have to take into account the work to get that index, as multiplying has a slight cost too.  Much better to just do it the easier way and let the compiler speed it up.

About understanding and using arrays, do it as it makes sense to you.  If your game world is a 2d flat plane, then it makes good sense to use a 2d array to represent the tiles, unless you need a different structure, like maybe a 2d grid(as in the dynamic version).  As far as programming them goes, which index is 'x' and which one is 'y' doesn't really matter much, as long as all the code uses them the same way.  So like I said above, go with whatever makes sense and is easier to implement and understand later.

Share on other sites

I don't know about C#, but as far as I recall, Java multidimensional arrays are not contiguous in memory.

Note Servant of the Lord linked to a C++ specific topic.

Java arrays can be allocated on a per-line basis and don't even have to be the same size. At the same time, they cannot be linearized, unless they are born linear.

So, the concept is not to "be applied to any language out there". There are specific behaviors.

Share on other sites

Hi,

I asked a similar Question on here the other week regarding using a 1D array for bitmap storage, using x and y values to write to pixels.

Here's what I was taught to remember:

lets say C:

const int width= 10;

const int height = 10;

int * array_[ width - 1 * height - 1 ] ;

//write to the index value 1D array but take 2D array indices x and y

void modify( int x, int y, int index )

{

array_[ (x * width) + y ] = index ;

}

int main()

{

int x_ = 2;

int y_ = 3;

// Go up in the lift then walk along the corridor

int cur_index = ( x_ * width ) + y_ ;

//So you could loop through this 1D array as if it were a 2D array like so:

for( int x = 0; x < height; x += width )

for( int y = 0; y < width; y++ )

modify( x, y, (x * width) + y ) ;

return 0;

}

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• Forum Statistics

• Total Topics
633769
• Total Posts
3013754
×