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

What's their difference? (1) yields an error, what's the reason?

And

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

using (2) will prompt an error. Why?

Ronny Brendel
  • 4,777
  • 5
  • 35
  • 55
william007
  • 17,375
  • 25
  • 118
  • 194
  • 1
    What is your second piece of code supposed to do anyway? It doesn't make any sense. – harold Sep 24 '12 at 14:46
  • Assign an array of the same size over..is there a way to do this? – william007 Sep 24 '12 at 14:47
  • what do you mean, something like `double[,] d = new double[9,9];`? Oh wait I get what you mean, I think. If you mean "is there a way to initialize an array of arrays all at once", then no, you can't do that. – harold Sep 24 '12 at 14:48
  • 2
    The first sample (`[][]`) is usually called a 'jagged array' but when you call it an 'array of array' the problem is easier to understand. – H H Sep 24 '12 at 14:49
  • 1
    For the record: `double d = new double[9];` should be: `double[] d = new double[9];` – Ronnie 'Madolite' Solbakken May 07 '18 at 01:41

5 Answers5

569

One is an array of arrays, and one is a 2d array. The former can be jagged, the latter is uniform.

That is, a double[][] can validly 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 entry in the array is a reference to an array of double. With a jagged array, you can do an assignment to an array like you want in your second example:

x[0] = new double[13];

On the second item, because it is a uniform 2d array, you can't assign a 1d array to a row or column, because you must index both the row and column, which gets you down to a single double:

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

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

UPDATE:

To clarify based on your question, the reason your #1 had a syntax error is because you had this:

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

And you can't specify the second index at the time of construction. The key is that ServicePoint is not a 2d array, but an 1d array (of arrays) and thus since you are creating a 1d array (of arrays), you specify only 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];
starball
  • 20,030
  • 7
  • 43
  • 238
James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
238

In the first instance you are trying to create what is called a jagged array.

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

The above statement would have worked if it was defined like below.

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

what this means is you are creating an array of size 10 ,that can store 10 differently sized arrays inside it.In simple terms an Array of arrays.see the below image,which signifies a jagged array.

enter image description here

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

The second one is basically a two dimensional array and the syntax is correct and acceptable.

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

And to access or modify a two dimensional array you have to pass both the dimensions,but in your case you are passing just a single dimension,thats why the error

Correct usage would be

ServicePoint[0][2] ,Refers to an item on the first row ,third column.

Pictorial rep of your two dimensional array

enter image description here

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
71

double[][] are called jagged arrays , The inner dimensions aren’t specified in the declaration. Unlike a rectangular array, each inner array can be an arbitrary length. Each inner array is implicitly initialized to null rather than an empty array. Each inner array must be created manually: Reference [C# 4.0 in nutshell The definitive 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;
}

double[,] are called rectangular arrays, which are declared using commas to separate each dimension. The following piece of code declares a rectangular 3-by-3 two-dimensional array, initializing it with numbers 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;
FranciscoBouza
  • 590
  • 6
  • 19
Adil
  • 146,340
  • 25
  • 209
  • 204
25

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

double[][] ServicePoint = new double[10][];
Omar
  • 16,329
  • 10
  • 48
  • 66
  • Well, it does seem like half an answer. It's not wrong, but it only addresses a few of the issues in the OP. – Servy Sep 24 '12 at 16:19
9

double[][] is an array of arrays and double[,] is a matrix. If you want to initialize an array of array, you will need to do this:

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

Take in account that using arrays of arrays will let you have arrays of different lengths:

ServicePoint[0] = new double[10];
ServicePoint[1] = new double[3];
ServicePoint[2] = new double[5];
//and so on...
Ivo
  • 8,172
  • 5
  • 27
  • 42
  • long[,] data = new long[2, 4]{ {1,2,3,4}, {7,8,9,10} }; and long[][] data2 = new long[2][] { new long[4]{1,2,3,4}, new long[4]{7,8,9,10} }; have the same results. However, one is an array of array longs and the other is a 2 dimensional array of longs – Golden Lion Dec 31 '19 at 14:58