-3

I want to make a multidemesional array of jagged arrays. Is this possible? How?

For Instance I see lokts of examples like the following:

int[][,] jaggedArray4 = new int[3][,] 

I want to create the following:

int[,,][] myFixedJagged = new int[2,2,3][]

where the last [] is Jagged. How can I declare that?

Thanks!

Gullie667
  • 133
  • 11

2 Answers2

2

This just works:

int[,,][] myFixedJagged = new int[2, 2, 3][];
myFixedJagged[0, 0, 0] = new int[10];
myFixedJagged[0, 0, 0][9] = 1;

I wouldn't want to use it though.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Exactly. Note that in C# an array `int[,,][]` is a three-dimensional array whose element type is `int[]`, a one-dimensional array of ints. The brackets are written in the opposite order by the runtime. For example `myFixedJagged.GetType().ToString()` or equivalently `typeof(int[,,][]).ToString()` gives something like `"System.Int32[][,,]"` where `Int32[]` is an array type which is the element type of the "outer" (in CLR notation) array. – Jeppe Stig Nielsen Dec 19 '15 at 11:40
1

Is this what you want?

static void Main(string[] args)
{
    // This is the silliest thing I have seen in my life
    int[, ,][][] jgarray=new int[2, 2, 3][][];

    for (int i=0; i<2; i++)
    {
        for (int j=0; j<2; j++)
        {
            for (int k=0; k<3; k++)
            {
                var array =new int[10][];

                for (int z=0; z<10; z++)
                {
                    array[z]=new int[20];
                    for (int v=0; v<20; v++)
                    {
                        array[z][v]=v+20*(z+10*(k+3*(j+2*i)));
                    }
                }

                jgarray[i, j, k]=array;
            }
        }
    }
}

pic

John Alexiou
  • 28,472
  • 11
  • 77
  • 133