How to declare 2D arrays in Haxe?
Asked Answered
S

5

15

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.

Sale answered 1/5, 2013 at 11:47 Comment(1)
You might want to mark one of the answers as accepted?Shine
C
13

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.

Corvette answered 1/5, 2013 at 22:50 Comment(0)
K
8

As you figured in the comment on John's answer, there is no built-in for 2d arrays that I know of, but it's not hard to create one.

Here I've made 2 helper functions, one uses haxe.ds.Vector, which is new in Haxe 3 and is optimised for fixed size collections. The other uses normal arrays, so may be slower on some platforms, and technically isn't fixed width, just initialised to a certain size.

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;
    }
}

The Vector2D will only work on Haxe 3 (released later this month), Array2D should work fine on Haxe 2 also.

Koan answered 1/5, 2013 at 13:4 Comment(0)
S
6

You can fake a 2D array by using a 1D array:

class Array2 extends Array 
{ 
    public var pitch(default, null): Int; 
    public function new(x: Int, y: Int) 
    { 
        pitch = x; 
        super(x * y); 
    } 

    public function get(x: Int, y: Int) 
    { 
        return this[y * pitch + x]; 
    } 
} 
Smith answered 1/5, 2013 at 11:54 Comment(3)
Wait, I'm totally new to Haxe, are you saying that there's no simple 2d array init in Haxe ? That's interestingSale
Nope.. there really isn't. The way you show us in your OP is how it should be done, but there are many helper functions that can simplify it for you. (See Jason's answer)Smith
Basic types cannot be extended in Haxe, this will not work. You need to give a parameter also anyway.Inconclusive
W
2

If you're not using haxe 3 and you want an array with nulls the shortest would be to init only the last element, the others will be filled with null:

var arr2d = new Array<Array<AType>>();
for (i in 0...10) { arr2d[i] = []; arr2d[i][10] = null; }

or like this to init with a value:

for (i in 0...10) for (j in 0...10) arr2d[i][j] = a_value;
Wombat answered 6/5, 2013 at 7:21 Comment(0)
A
2

this is a simple way to declare array in axe ( 3.0 )

 class Prova
 {
   static function main ( )
   {
     var arr : Array< Array<Int> > = [  [],[],[]   ] ;

     arr[0][0] = 10 ;
     arr[0][1] = 11 ;

     trace ( arr[0] );
     trace ( arr[1] ) ;
     trace ( arr[2] ) ;
     return 0 ;
   }
 }

output :

Prova.hx:12: [10,11]

Prova.hx:13: []

Prova.hx:14: []

Annulment answered 1/7, 2013 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.