Home All Groups Group Topic Archive Search About

How to initialize jagged array?

Author
11 Mar 2006 3:12 AM
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?

Thanks in advance.

Author
11 Mar 2006 7:33 AM
Jon Skeet [C# MVP]
deko <deko@nospam.com> wrote:
> 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]

<snip>

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
11 Mar 2006 11:18 AM
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.

Bookmark and Share