silentcallz
silentcallz

Reputation: 111

An array initializer of the length of 2 is expected

I'm trying to get an AI to learn the and function but this 3d array is not working out

      int[, ,] inputs =
         {
            { { 0, 0 }, {0} },
            { { 0, 1 }, { 0 } },
            { { 1, 0 }, { 0 } },
            { { 1, 1 }, { 1 } }

        };

Upvotes: 2

Views: 7663

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500675

You've declared a rectangular array - although I suppose "cuboid" array would be more appropriate in this case. But you have to make each "sub-array" initializer the same length. To continue the geometric metaphor, every column has to be the same length - but you've got some of length 2 and some of length 1.

So this will compile, for example:

int[, ,] inputs =
{
    { { 0, 0 }, { 0, 0 } },
    { { 0, 1 }, { 0, 0 } },
    { { 1, 0 }, { 0, 0 } },
    { { 1, 1 }, { 1, 0 } }

};

That's now a 4 x 2 x 2 array.

If you want to have every "final sub-array" able to be a different length, you could have a rectangular array of one-dimensional arrays:

int[,][] inputs =
{
    { new[] { 0, 0 }, new[] { 0 } },
    { new[] { 0, 1 }, new[] { 0 } },
    { new[] { 1, 0 }, new[] { 0 } },
    { new[] { 1, 1 }, new[] { 1 } }             
};

Upvotes: 3

mihkov
mihkov

Reputation: 1189

int[, ,] inputs = new int[sizeX, sizeY, sizeZ];
for(int x = 0; x < inputs.GetLength(0); x++)
{
    for(int y = 0; y < inputs.GetLength(1); y++)
    {
        for(int z = 0; z < inputs.GetLength(2); z++)
        {
            int element = inputs[x, y, z];
        }
    }
}

Upvotes: -1

Related Questions