# [][] versus [,] Multidimensional Array [duplicate]

## Problem

``````double[][] ServicePoint = new double[10][9]; // <-- gives an error (1)
double[,] ServicePoint = new double[10,9]; // <-- ok (2)
``````

What’s the difference between them? What is the reason for (1) returning an error?

And

``````double d = new double[9]
ServicePoint[0] = d;
``````

(2) will result in an error. Why?

## Solution #1

One is a 2d array, whereas the other is an array of arrays. The former can be splintered, whereas the latter is smooth.

In other words, a double[][] can be:

``````double[][] x = new double[5][];

x[0] = new double[10];
x[1] = new double[5];
x[2] = new double[3];
x[3] = new double[100];
x[4] = new double[1];
``````

Because each element of the array is a reference to a double array. You may do an assignment to an array with a jagged array, just like you did in your second example:

``````x[0] = new double[13];
``````

Because it’s a uniform 2d array, you can’t assign a 1d array to a row or column because you’d have to index both the row and column, reducing it to a single double:

``````double[,] ServicePoint = new double[10,9];

ServicePoint[0]... // <-- meaningless, a 2d array can't use just one index.
``````

UPDATE:

``````double[][] ServicePoint = new double[10][9];
``````

Furthermore, the second index cannot be specified at the time of construction. The key is that ServicePoint is a 1d array (of arrays) rather than a 2d array, therefore you only need to supply one index:

``````double[][] ServicePoint = new double[10][];
``````

Then, when you create each item in the array, each of those are also arrays, so then you can specify their dimensions (which can be different, hence the term jagged array):

``````ServicePoint[0] = new double[13];
ServicePoint[1] = new double[20];
``````

Hope that helps!

## Solution #2

In the first instance, you’re attempting to generate a jagged array.

``````double[][] ServicePoint = new double[10][9].
``````

If the above sentence had been defined as follows, it would have worked.

``````double[][] ServicePoint = new double[10][]
``````

What this means is that you’re making a ten-dimensional array that can hold ten different-sized arrays. In layman’s terms, it’s an Array of Arrays. See the figure below, which represents a jagged array.

http://msdn.microsoft.com/en-us/library/2s05feca(v=vs.80).aspx

The second is essentially a two-dimensional array, using correct and acceptable syntax.

``````  double[,] ServicePoint = new double[10,9];//<-ok (2)
``````

You must pass both dimensions, but you are only passing one, which is why you are getting an error.

The proper application would be

[0][2] ServicePoint, Refers to a third-column item in the first row.

Visual representation of your two-dimensional array

## Solution #3

The inner dimensions of double[][] aren’t specified in the declaration, hence they’re called jagged arrays. Each inner array, unlike a rectangular array, can be any length. Instead of an empty array, each inner array is implicitly initialized to null. Each inner array must be explicitly created: [C# 4.0 in a Nutshell] as a reference [The Ultimate Reference]

``````for (int i = 0; i < matrix.Length; i++)
{
matrix[i] = new int [3]; // Create inner array
for (int j = 0; j < matrix[i].Length; j++)
matrix[i][j] = i * 3 + j;
}
``````

Rectangular arrays, which are defined with commas to separate each dimension, are known as double[,]. The code below declares a rectangle 3-by-3 two-dimensional array and fills it with values ranging from 0 to 8:

``````int [,] matrix = new int [3, 3];
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
matrix [i, j] = i * 3 + j;
``````

## Solution #4

The syntax is: double[,] is a 2d array (matrix), while double[][] is an array of arrays (jagged arrays), and the syntax is: double[,] is a 2d array (matrix).

``````double[][] ServicePoint = new double[10][];
``````

## Solution #5

Double[,] is a matrix and double[][] is an array of arrays. If you wish to create an array of arrays, follow these steps:

``````double[][] ServicePoint = new double[10][]
for(var i=0;i<ServicePoint.Length;i++)
ServicePoint[i] = new double[9];
``````

Keep in mind that utilizing arrays of arrays allows you to have arrays of various lengths:

``````ServicePoint[0] = new double[10];
ServicePoint[1] = new double[3];
ServicePoint[2] = new double[5];
//and so on...
``````