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

        };
Jon Skeet
people
quotationmark

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

people

See more on this question at Stackoverflow