Why we have both jagged array and multidimensional array?

c# multidimensional array vs jagged array performance
difference between jagged array and rectangular array in c#
difference between jagged array and multidimensional array in java
jagged array of 2d arrays
c# multidimensional array performance
sort jagged array c#
array of arrays
whats a jagged array
  1. What is the difference between jagged array and Multidimensional array. Is there a benefit of one on another?

  2. And why would the Visual Studio not allow me to do a

    MyClass[][] abc = new MyClass[10][20];
    

    (We used to do that in C++, but in C# it underlines [20] with red wriggly line.. Says invalid rank specifier)

    but is happy with

    MyClass[,] abc = new MyClass[10,20];
    
  3. Finally how can I initialize this in a single line (like we do in simple array with {new xxx...}{new xxx....})

    MyClass[][,][,] itemscollection;
    
  1. A jagged array is an array-of-arrays, so an int[][] is an array of int[], each of which can be of different lengths and occupy their own block in memory. A multidimensional array (int[,]) is a single block of memory (essentially a matrix).

  2. You can't create a MyClass[10][20] because each sub-array has to be initialized separately, as they are separate objects:

    MyClass[][] abc = new MyClass[10][];
    
    for (int i=0; i<abc.Length; i++) {
        abc[i] = new MyClass[20];
    }
    

    A MyClass[10,20] is ok, because it is initializing a single object as a matrix with 10 rows and 20 columns.

  3. A MyClass[][,][,] can be initialized like so (not compile tested though):

    MyClass[][,][,] abc = new MyClass[10][,][,];
    
    for (int i=0; i<abc.Length; i++) {
        abc[i] = new MyClass[20,30][,];
    
        for (int j=0; j<abc[i].GetLength(0); j++) {
            for (int k=0; k<abc[i].GetLength(1); k++) {
                abc[i][j,k] = new MyClass[40,50];
            }
        }
    }
    

Bear in mind, that the CLR is heavily optimized for single-dimension array access, so using a jagged array will likely be faster than a multidimensional array of the same size.

Why we have both jagged array and multidimensional array?, A jagged array is an array-of-arrays, so an int[][] is an array of int[] , each of which can be of different lengths and occupy their own block in� The method Length returns the number of arrays contained in the jagged array. For example, assuming you have declared the previous array, this line: System.Console.WriteLine(jaggedArray4.Length); returns a value of 3. Example. This example builds an array whose elements are themselves arrays. Each one of the array elements has a different size.

A jagged array is an array of arrays. Each array is not guaranteed to be of the same size. You could have

int[][] jaggedArray = new int[5][];
jaggedArray[0] = { 1, 2, 3 }; // 3 item array
jaggedArray[1] = new int[10]; // 10 item array
// etc.

It's a set of related arrays.

A multidimensional array, on the other hand, is more of a cohesive grouping, like a box, table, cube, etc., where there are no irregular lengths. That is to say

int i = array[1,10];
int j = array[2,10]; // 10 will be available at 2 if available at 1

MultiDimensional Arrays And Jagged Arrays In C#, Multidimensional arrays are also known as Rectangular Arrays. In this tutorial, we will learn about Multi-Dimensional Arrays and Jagged Arrays in C# of length 5 and both of these can be stored in the same jagged array. In the previous chapter, we learned about a single dimensional array. We can also have multidimensional arrays in C#. Suppose there are 50 students in a class and each student is studying 5 subjects. We can make a two-dimensional array to store marks of each student in each subject. Multidimensional Array

A rectangular array always has the same amount of columns for every row.

MyClass[,] x = new MyClass[10,30]

Every row has 30 columns, whereas in a jagged array, this is not required. Therefore, I think you'll have to initialize every 'row' in a jagged array separately:

MyClass[][] x = new MyClass[10][];

for(int i = 0; i < 10; i++)
{
    x[i] = new MyClass[30];
}

In fact, this means that not every row in the jagged array must contain the same number of elements. (In my example, it does have the same number of elements, but this is not required).

You can perfectly do this, for instance:

MyClass[][] x = new MyClass[10][];

for(int i = 0; i < 10; i++)
{
    x[i] = new MyClass[(30 + i)];
}

This might be an interesting article for you.

What are the differences between a multi-dimensional array and , Multi-dimensional arrayMulti-dimensional arrays are also called rectangular array. You can define a 3-dimensional array of integer as −int [ ,� Jagged Array can also be mixed with multidimensional arrays. Here, the number of rows will be fixed at the declaration time, but you can vary the number of columns. Declaration. In Jagged arrays, user has to provide the number of rows only. If the user is also going to provide the number of columns, then this array will be no more Jagged Array

Ad 3) To initialize such a monster like [][,][,], you can do something like:

        int [,][,] multiArr1 = { { new int[,] { { 2, 2 }, { 1, 1 } },
                                     new int[,] { { 2, 2 }, { 1, 1 } } },
                                     { new int[,] { { 2, 2 }, { 1, 1 } },
                                         new int[,] { { 2, 2 }, { 1, 1 } } } };
        int [,][,] multiArr2 = { { new int[,] { { 2, 2 }, { 1, 1 } },
                                     new int[,] { { 2, 2 }, { 1, 1 } } },
                                     { new int[,] { { 2, 2 }, { 1, 1 } },
                                         new int[,] { { 2, 2 }, { 1, 1 } } } };

        int [][,][,] superMultiArray = { multiArr1, multiArr2 };

C#, The elements of Jagged Array are reference types and initialized to null by default. Jagged Array can also be mixed with multidimensional arrays. Here, the number of rows will be fixed at the declaration time, but you can vary� Before we learn about the multidimensional array, make sure you know about Java array. A multidimensional array is an array of arrays. Each element of a multidimensional array is an array itself. For example, int[][] a = new int[3][4]; Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum

If you're looking for a multi-dimensional array that has set bounds, always use the [,] style syntax. This will make sure that each portion is equally sized.

When you use [][] what is really going is that you're creating an array of arrays. This then means that each array can be sized differently. For example:

int[][] jaggedArray = new int[5][]
for(int index = 0; index < jaggedArray.Length ; ++index)
{
    jaggedArray[index] = new int[index + 1];
}

Jagged Arrays, A jagged array in C# is an array whose elements are arrays of different dimensions and sizes. You can also initialize the array upon declaration like this: C# Copy It is possible to mix jagged and multidimensional arrays. It builds up a small jagged array, and then iterates through it. Part 1: First we create a jagged array of int elements. The array has 3 rows—so it can have 3 subarrays once they are all assigned. Int, uint. Part 2: We assign the indexes to new int arrays. We only have an array of empty references—new arrays must be created for the rows.

C# Jagged Array vs multidimensional Array, Compares C# .NET jagged arrays to multidimensional arrays (rectangular array).Duration: 7:26 Posted: Nov 4, 2013 A jagged array is an array of arrays. i.e. it stores arrays as its values. The jagged array will throw out of range exception if the specified index doesn’t exist. Conclusion. In this tutorial, we learned about Jagged and Multidimensional arrays in C#. We learned how to declare and initialize a two-dimensional array.

4. Pointers and Arrays, We will also examine how to create jagged arrays in C, although they are In the section Pointers and Multidimensional Arrays, we will examine this and pointer arithmetic with both the array name and the pointer for various values. A multidimensional array creates a nice linear memory layout while a jagged array implies several extra levels of indirection. Looking up the value jagged[3][6] in a jagged array var jagged = new int[10][5] works like this: Look up the element at index 3 (which is an array) and look up the element at index 6 in that array (which is a value).

Unity: Single vs multidimensional vs jagged arrays : VoxelGameDev, I've always been lead to believe the single array should be fastest but the results were. array of both of these also slows everything down compared to jagged which is And if you aren't using jagged I suggest it is something worth testing. The declaration of multidimensional array can be done by adding [] for each dimension to a regular array decleration. For instance, to make a 2-dimensional int array, add another set of brackets to the declaration, such as int[][]. This continues for 3-dimensional arrays (int[][][]) and so forth.

Comments
  • The whole point of a jagged array is that the "nested" arrays needn't be of uniform size.
  • msdn.microsoft.com/en-us/library/2yd9wwz4(v=vs.71).aspx - Multidimensional array syntax as [X,Y] is valid according to docs
  • Additional sub-question: Is it possible to use foreach() with multi-dimensional array?
  • @Serge - of course, as Array implements IEnumerable. You could always try it and see for yourself :)
  • possible duplicate of What is differences between Multidimensional array and Array of Arrays in C#?
  • can you point us to some evidence that single dimensional array accesses are faster?
  • @GreyCloud - stackoverflow.com/questions/597720/…
  • Is there a (common) use case for the multi-dimensional array?
  • Examples: checkered board var board = new Piece[8, 8];, a transformation matrix var m = new double[2, 2];.
  • I tried your code. It did not compile. Try adding int[3] so Try jaggedArray[0] = int[3]{ 1, 2, 3 };
  • I know this is old, but just for informational purposed int[3] is not neccessary. a simple int[] is all that matters. int[][] myArray = new int[5][]; myArray[0] = new int[] {1, 2, 3, 4}; This is all that is neccessary.