Why are 2-D arrays reversed?

Chris Katko ckatko at gmail.com
Wed Oct 10 13:22:41 UTC 2018


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.


More information about the Digitalmars-d-learn mailing list