开发者

2D array indeces question

I am quite confused, if I create array [5,10] then it means it has 5 rows and 10 columns? So its Y,X, right? Confusing to me as I a开发者_开发百科m used to work with X,Y order.


Well, row, column, X, Y, that's all up to interpretation isn't it?

[5,10] creates an array that contains 5 items. Each item is a 10 member array.

[10,5] creates an array that contains 10 items. Each item is a 5 member array.

I hope this clears things up.


What you're actually creating with the above syntax is called a "multidimensional array", or an array of arrays. Specifically, you're creating an array that contains 5 items, and each of those 5 items itself contains 10 items.

A single dimensional array can be seen as a single row of elements:

2D array indeces question

A bidimensional array (the most common case of multidimensional array) can therefore be seen as simply adding another row of elements, producing the table visualization that you're familiar with:

2D array indeces question

However, you can mentally construct that table however you wish. If you're more comfortable with the (X, Y) syntax, use that instead.

Remember though, that multidimensional arrays are not limited to only two indices. That makes them more difficult to conceptualize as a table as you increase the number of indices.


Unless you are processing a huge array in a tight loop (so that memory caching is affected), how you interpret the dimensions in an array doesn't matter.

There is no special case for a two dimensional array that forces you to use [y,x] rather than [x,y]. Consider that a one dimensional array can represent a row, a column, or something completely different. A three dimensional array doesn't have to represent the three spacial dimensions.

I believe that the more intuitive way of using the indices for most users when a two dimensional array represents a grid, is [x,y].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜