正如您在对 John 的回答的评论中发现的那样,据我所知,没有内置的二维数组,但创建一个并不难。
这里我制作了 2 个辅助函数,一个使用 haxe.ds.Vector,这是 Haxe 3 中的新功能,针对固定大小的集合进行了优化。另一个使用普通数组,因此在某些平台上可能会更慢,并且在技术上不是固定宽度,只是初始化为特定大小。
import haxe.ds.Vector;
class Vector2DTest
{
static function main()
{
// 2D vector, fixed size, sometimes faster
var v2d = Vector2D.create(3,5);
v2d[0][0] = "Top Left";
v2d[2][4] = "Bottom Right";
trace (v2d);
// [[Top Left,null,null,null,null],[null,null,null,null,null],[null,null,null,null,Bottom Right]]
// 2D array, technically variable size, but you'll have to initialise them. Sometimes slower.
var a2d = Array2D.create(3,5);
a2d[0][0] = "Top Left";
a2d[2][4] = "Bottom Right";
trace (a2d);
// [[Top Left,null,null,null,null],[null,null,null,null,null],[null,null,null,null,Bottom Right]]
}
}
class Vector2D
{
public static function create(w:Int, h:Int)
{
var v = new Vector(w);
for (i in 0...w)
{
v[i] = new Vector(h);
}
return v;
}
}
class Array2D
{
public static function create(w:Int, h:Int)
{
var a = [];
for (x in 0...w)
{
a[x] = [];
for (y in 0...h)
{
a[x][y] = null;
}
}
return a;
}
}
Vector2D 仅适用于 Haxe 3(本月晚些时候发布),Array2D 应该也适用于 Haxe 2。