14

What is the difference between:

int [][] myArray;

and

int [,] myOtherArray;
George Stocker
  • 57,289
  • 29
  • 176
  • 237
Rita
  • 911
  • 4
  • 16
  • 31
  • 2
    Note that mixing the two techniques -- making a ragged array of multidimensional arrays -- is legal but almost certainly a bad idea. It makes the code hard to read. See my article on the subject for details. http://blogs.msdn.com/b/ericlippert/archive/2009/08/17/arrays-of-arrays.aspx – Eric Lippert Mar 22 '11 at 23:40

7 Answers7

21

The first is a jagged array: an array where each item in the array is another array

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];

The second is a multidimensional array, aka a matrix.

int[,] array = new int[4, 2]; // create a 4 by 2 matrix
JoDG
  • 1,326
  • 8
  • 18
17

myArray is a jagged array, or an array of arrays. Each element of myArray is itself an int[].

myOtherArray is a rectangular (or multidimensional) array - a single object containing all the data directly.

Which you should use really depends on the situation. Sometimes it can be handy to have an array for each "row" of data (with the ability to replace whole rows, and have rows with different lengths), whereas at other times it makes sense to force a uniform layout.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • can you please either elaborate on or provide resources regarding where which use is more appropriate? – smartcaveman Mar 22 '11 at 20:27
  • 3
    @smartcaveman: Not easily, I'm afraid. There will be pros and cons which are very situation-specific. If you give me a particular situation I could give some thoughts though. – Jon Skeet Mar 22 '11 at 21:05
9

I found the best way to understand it was to see a graphical representation of it =)

int[][] jagged = new int[3][];
jagged[0] = new int[1];
jagged[1] = new int[2];
jagged[2] = new int[4];

will look like this

[0] - [0]
[1] - [0][1]
[2] - [0][1][2][3]

while a two+ dimensional

int[,] twodimensional = new int[3,4]

will look like this

[0,0][0,1][0,2][0,3]
[1,0][1,1][1,2][1,3]
[2,0][2,1][2,2][2,3]
Moulde
  • 3,438
  • 4
  • 29
  • 38
2

The first is a jagged array, the other multi-dimensional - the difference is that the elements of a jagged array can be of different dimensions and sizes.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
2

Jagged array:

int [][] myArray;

Rectangular array:

int [,] myOtherArray;

Quote Comparing Rectangular and Jagged Arrays :

The structure of rectangular and jagged arrays is significantly different.

One-dimensional arrays have specific instructions in the CIL that allow them to be optimized for performance. Rectangular arrays do not have these instructions, and are not optimized to the same level. Because of this, it can sometimes be more efficient to use jagged arrays of one-dimensional arrays—which can be optimized—than rectangular arrays, which cannot. On the other hand, the programming complexity can be less for a rectangular array because it can be treated as a single unit, rather than an array of arrays.

Four
  • 900
  • 1
  • 10
  • 18
0

This has to be a duplicate. One is a jagged array, one is a two-dimensional array. You should be able to take it from there.

harpo
  • 41,820
  • 13
  • 96
  • 131
0

Both statements declare uninitialized multi-dimensional arrays of ints. The first is a jagged array and the second is 2-dimensional.

You can initialize the 2-dimensional array at the same time as you declare it as follows:

int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

OR,

int[,] array = new int[4, 2];

Refer to the official documentation:

Stecya
  • 22,896
  • 10
  • 72
  • 102
Winger
  • 676
  • 3
  • 7