Why are 2-D arrays reversed?

Steven Schveighoffer schveiguy at gmail.com
Wed Oct 10 16:00:42 UTC 2018


On 10/10/18 9:22 AM, Chris Katko wrote:
> int[][] data =
>      [
>          [1, 0, 1, 0, 0],
>          [1, 0, 1, 0, 0],
>          [1, 0, 1, 1, 1],
>          [1, 0, 0, 1, 0],
>          [5, 1, 1, 1, 0]
>      ];
> 
> when drawn with data[i][j], prints the transpose of "data":
> 
> [1, 1, 1, 1, 5]
> [0, 0, 0, 0, 1]
> [1, 1, 1, 0, 1]
> [0, 0, 1, 1, 1]
> [0, 0, 1, 0, 0]
> 
> So, if I flip [i][j] and print a row of "j's", it'll be correct. It's 
> very confusing and counter-intuitive to have to remember to swap i and j 
> every time I use an array.
> 
> I guess when I load data from files, the i/j are already swapped and 
> stay consistent, but when using an array in source code, they have to be 
> flipped.

I'm not sure what code you are using, but it prints out just fine for me:

https://run.dlang.io/is/hrA0tj

-Steve


More information about the Digitalmars-d-learn mailing list