TheOtherGuy
TheOtherGuy

Reputation:

What's the difference between array[][] and array[,]?

Can someone explain the difference between these two declarations?

  1. double dArray[][];
  2. double dArray[,];

Upvotes: 4

Views: 237

Answers (4)

Marlon
Marlon

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

Ritch Melton
Ritch Melton

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

user541686
user541686

Reputation: 210402

See the official documentation (click on the C# tab).

Upvotes: 1

Bala R
Bala R

Reputation: 108947

double dArray[][];

is Array-of-arrays while

double dArray[,];

is a two dimentional array.

it's easy enough to look them up.

MSDN Reference link

Upvotes: 7

Related Questions