3

How to create a jagged array that consists of two 2d array? please help. Thank you.

int[][] jaggedArray = new int[3][];

the above code creates a single-dimensional array that has three elements, each of which is a single-dimensional array of integers. Can any one help me in creating a two 2d array.

3
  • what do you mean, that every item in the jagged array is instantiated to the same dimension? Commented Oct 30, 2014 at 15:16
  • i want the the 2d array with the different row length. Is it possible? Commented Oct 30, 2014 at 15:18
  • 1
    do you mean a "2d Multi-Dimensional Array" or a "2 Dimensional Jagged Array" or something else? Commented Oct 30, 2014 at 15:36

3 Answers 3

2

I think you want something like this,

var jaggedArray = new[]
        {
            new[] { 1 },
            new[] { 1, 2 ,3 },
            new[] { 1, 2 }
        };

this creates a "jagged" array, with two dimensions where each "row" has a different length.

All of the following assertions would be True.

jaggedArray.Length == 3
jaggedArray[0].Length == 1
jaggedArray[1].Length == 3
jaggedArray[2].Length == 2

If you knew the lengths were fixed but, didn't know the data, you could do,

var jaggedArray = new[] { new int[1], new int[3], new int[2] };

Following on from you comment, maybe you want something like this,

var jaggedArray1 = new[]
        {
            new[] { 1, 2, 3, 4 },
            new[] { 1, 2, 3 },
            new[] { 1, 2 }
        };

var jaggedArray2 = new[]
        {
            new[] { 1, 2, 3 },
            new[] { 1, 2, 3, 4 }
        };

int[][][] jaggedArray = new[]
        {
            jaggedArray1,
            jaggedArray2
        };

you could just do,

var jaggedArray = new[]
        {
            new[]
                {
                    new[] { 1, 2, 3, 4 },
                    new[] { 1, 2, 3 },
                    new[] { 1, 2 }
                },

            new[]
                {
                    new[] { 1, 2, 3 },
                    new[] { 1, 2, 3, 4 }
                }
        };
Sign up to request clarification or add additional context in comments.

4 Comments

Yes i want some thing like this. Can i implement in this way? var jaggedArray1 = new[] { new[] { 1,2,3,4}, new[] { 1, 2 ,3 }, new[] { 1, 2 } }; var jaggedArray2 = new[] { new[] { 1,2,3}, new[] { 1, 2 ,3,4 }, }; int[] jaggedArray = new int[2][]; jaggedArray[0] = jaggedArray1; jaggedArray[1] = jaggedArray2;
@UditSaikia looks syntactically correct but, its hard to tell in a comment.
it gives an error Error 1 Cannot implicitly convert type 'int[][]' to 'int[]'
@UditSaikia, now that I've added your comment to my question, that makes sense, see the edit.
2

What about this:

int[][,] jaggedArray = new int[3][,];

The , creates the 2D array in the jagged array. Read more on Multi-dimensional arrays on MSDN.

Next, you have to initialize every 2D array inside that array:

int[,] 2dArray1 = new int[2,3];
jaggedArray[0] = 2dArray1;

int[,] 2dArray2 = new int[4,5];
jaggedArray[1] = 2dArray2;

And so on.

10 Comments

but why would you do that, instead of int[3][][]
@Jodrell: That is a jagged array, not a 2 dimensional array.
Can the row size be of different length?
Sure, it's your party.
@UditSaikia of course, in both a 3 dimensional jagged array and a collection of multi-dimensional arrays.
|
1

The second pair of brackets indicates the dimensions. So it's like you are declaring a multi-dimensional array except you don't need to specify dimensions in the definition.You can initialize each array with different dimensions.

int[][,] jaggedArray = new int[3][,];

1 Comment

but why would you do that, instead of int[3][][]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.