9

Coming from a perl background, I have always defined a 2D array using int[][]. I know you can use int[,] instead so what are the differences?

Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
  • 1
    Here are some thoughts in terms of performance: http://stackoverflow.com/questions/168897/whats-better-in-regards-to-performance-type-or-type – bruno conde Jul 21 '09 at 13:39

9 Answers9

29

The difference here is that the first sample, int[][] creates a jagged array, while the second creates a rectangular array (of dimension 2). In a jagged array each "column" can be of a different size. In a true multidimensional array, each "column" (in a dimension) is the same size. For more complete information see the Array section of the C# Programming Guide.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
12

Here's a good comparison

Basically int[][] is a "jagged" array, it looks like this:

[] -> [1, 2, 3]
[] -> [1, 2]
[] -> [1, 2, 3, 4]

While int[,] is a multidimentional array which always has the same width and height:

[1, 2, 3, 4
 5, 6, 7, 8
 9, 1, 2, 3]

Each have their uses depending on what you're trying to accomplish.

Community
  • 1
  • 1
Tom Ritter
  • 99,986
  • 30
  • 138
  • 174
  • 1
    Jagged--each 'row' can have different 'column' lengths. Multidimensional--each 'row' has the same 'column' length. –  Jul 21 '09 at 13:54
9

int[][] is an array of arrays or "jagged" array: you can use this when you want different sizes in the second dimension. For example, the first sub array can have 5 elements and the second can have 42.

int[,] is a two dimensional array: The second dimension is the same through out the array. With int[7, 42] the second dimension is 42 for all 7 lines.

jrcs3
  • 2,790
  • 1
  • 19
  • 35
4

int[][] is a jagged array, where int[,] is a two dimensional array.

plainly

var a = int[][]

allows you do have an array like this:

a[0] = new int[2];
a[1] = new int[5];

where as with int[,] you must always have the second portion of the array be the same:

var a = int[2,2];

a[0,0]
a[0,1]
a[1,0]
a[1,1]

you can't have a[2,2];

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
3

int[][] is called an array of arrays, it can have arbitrary length for each row.

int[,] is called rectangular array, where all the rows have the same length. it Can be simulated by the first one.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
1

The best way to understand the difference is to look at two ways to create an nxn matrix:

const int n = 8, m = 8;

int[,] matrix1 = new int[n,m];

int[][] matrix2 = new int[n][];
for (int i = 0; i < matrix2.Length; i++) { matrix2[i] = new int[m]; }

matrix1[1,1] = matrix2[2][2];

As you can see the second one is a bit more involved because you need the for-loop to fully create it. It is often called a jagged array because the 2nd order arrays do not need to be all of the same length.

H H
  • 263,252
  • 30
  • 330
  • 514
0

One thing to consider about a jagged array is that you are allocating non-contiguous chunks of memory. This is a good thing if you have a large array of large objects. I've seen SomeThing[n,n] create StackOverflow issues, but SomeThing[n][n] be ok.

Also, if an object gets > 85,000 bytes it goes to the LOH (Large Object Heap). .NET Collections and the Large Object Heap (LOH)

Community
  • 1
  • 1
Andy_Vulhop
  • 4,699
  • 3
  • 25
  • 34
0

Here's an excellent article about arrays, covering this topic very well.

Kenan E. K.
  • 13,955
  • 3
  • 43
  • 48
0

you can see int[][] as to (int[])[] (int[]) is a object

Edwin Tai
  • 1,224
  • 1
  • 13
  • 19