Reputation:
Can someone explain the difference between these two declarations?
double dArray[][];
double dArray[,];
Upvotes: 4
Views: 237
Reputation: 20314
The first is an array of double
arrays, meaning each separate element in dArray
can contain a different number of doubles depending on the length of the array.
double[][] dArray = new double[3][];
dArray[0] = new double[3];
dArray[1] = new double[2];
dArray[2] = new double[4];
Index
0 1 2
----- ----- -----
L 1 | | | | | |
e ----- ----- -----
n 2 | | | | | |
g ----- ----- -----
t 3 | | | |
h ----- -----
4 | |
-----
The second is called a multidimensional array, and can be thought of as a matrix, like rows and columns.
double[,] dArray = new dArray[3, 3];
Column
0 1 2
-------------
0 | | | |
R -------------
o 1 | | | |
w -------------
2 | | | |
-------------
Upvotes: 3
Reputation: 11598
The last syntax is easy, it declares a multidimensional array of doubles. Imagine the array is 3x2, then there would be 6 doubles in the array.
The 1st syntax declares a jagged array. The second syntax is rectangular or square, but this syntax need not be. You could have three rows, followed by 3 columns, then 2 columns, then 1 column, ie: its jagged.
2nd: 1-1, 1-2, 1-3
2-1, 2-2, 2-3
1st: 1-1, 1-2, 1-3
2-1, 2-2,
3-1,
Upvotes: 3
Reputation: 108947
double dArray[][];
is Array-of-arrays while
double dArray[,];
is a two dimentional array.
it's easy enough to look them up.
Upvotes: 7