Andrew Simpson
Andrew Simpson

Reputation: 7344

How to declare a multi-multi- dimensional array

Sounds simple and it probably is.

I have this variable:

byte[,,] data = new byte[360,288]

And I want 4 for them.

I do not want this though:

byte[,,,] data = new byte[360,288,4]

I prefer this:

byte[,,][] data = new byte[360,288][4]

Is it possible?

Upvotes: 1

Views: 109

Answers (1)

BlueTrin
BlueTrin

Reputation: 10113

Yes this is a special case of jagged arrays, one where one of the jagged dimension is multidimensional.

You should write something like this:

        // Initialise 4 arrays of two dimensional arrays
        byte[][,] data = new byte[4][,];
        // Initialise the arrays
        for (var i = data.GetLowerBound(0); i <= data.GetLowerBound(0); ++i)
            data[i] = new byte[360, 258];

Of course you can invert the dimensions if you need it.

        // Initialise 4 arrays of two dimensional arrays
        byte[,][] data2 = new byte[360,258][];
        // Initialise the arrays
        for (var i = data2.GetLowerBound(0); i <= data2.GetLowerBound(0); ++i)
            for (var j = data2.GetLowerBound(1); j <= data2.GetLowerBound(1); ++j)
                data2[i,j] = new byte[4];

Upvotes: 2

Related Questions