How to declare 2D arrays in Haxe?

sub_o picture sub_o · May 1, 2013 · Viewed 7.1k times · Source

In other programming languages, I can use int array[23][23] to declare a 2D array with 23 elements in each dimension. How do I achieve the same thing in Haxe?

Currently I need to do this:

var arr:Array<Array<Int>> = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];

But when the array grows to a larger size, it becomes infeasible for me to declare it like that anymore.

Answer

thedayturns picture thedayturns · May 2, 2013

The best way to do this is to take advantage of array comprehensions, provided in Haxe 3:

var bigArray:Array<Array<Int>> = [for (x in 0...10) [for (y in 0...10) 0]];

Array comprehensions are a really nice and condensed syntax for making arrays. The above code would make a 10x10 array, filled with 0s. You can read more about them here.

If you're running Haxe 2 for some reason, the best way to do it would be to fill them out with for loops, as suggested previously.