# How to initialize jagged array?

D

#### deko

I trying to create a jagged array of two arrays, with the second array being
an array of two-dimensional arrays.

A graphical representation might look like this:

x y[a,b] y[a,b] y[a,b]
x y[a,b]
x y[a,b] y[a,b]
x y[a,b] y[a,b] y[a,b] y[a,b]

I can initialize a jagged array of arrays like this:

string[][] str = new string[][]
{
new string[] {"loop for strings"},
new string[] {"loop for strings"},
};

which would give me this:

x y y y
x y
x y y
x y y y y

but how do I initialize the 2-Dim array on the "y" array?

I tried this:

string[][][,] str = new string[][][,]
{
new string[] {"loop for strings"},
new string[] {{"loop for strings"},
new string[,] {{"strD-1"}, {"strD-2"}}}
};

but no luck.

how do I initialize this type of array?

J

#### Jon Skeet [C# MVP]

deko said:
I trying to create a jagged array of two arrays, with the second array being
an array of two-dimensional arrays.

A graphical representation might look like this:

x y[a,b] y[a,b] y[a,b]
x y[a,b]
x y[a,b] y[a,b]
x y[a,b] y[a,b] y[a,b] y[a,b]
I tried this:

string[][][,] str = new string[][][,]
No - that's creating an array of arrays of rectangular arrays of
strings.

If you want something with a string and then an array of pairs, I
suggest you encapsulate that into your own type.

D

#### deko

string[][][,] str = new string[][][,]
No - that's creating an array of arrays of rectangular arrays of
strings.
I think it might be something more like this (still pseudo code):

string[][][,] str = new string[3][][,]
str[0] = new string[2][,]
str[0][0] = new string[,] { { "str00a" }, { "str00b" } }
str[0][1] = new string[,] { { "str01a" }, { "str01b" } }
str[1] = new string[1][,]
str[1][0] = new string[,] { { "str10a" }, { "str10b" } }
str[2] = new string[1][,]
str[2][0] = new string[,] { { "str20a" }, { "str20b" } }

which is intented to be a jagged array of multidimensional arrays.

But I'm not sure what the correct syntax is, and how I would fill such a
thing in a loop.
If you want something with a string and then an array of pairs, I
suggest you encapsulate that into your own type.
Yes, that makes sense.