30

Possible Duplicate:
What are the differences between using int[][] and int[,]?

I just came across this notation:

int[,]

Whenever i needed a matrix I used:

int[][]

What is the difference? When to use which one?

Edit: Thanks for the quick responses. I could've thought of that. It's also hard to google for stuff like this.

Now I understand the purpose. But how do they relate to System.Array?

Community
  • 1
  • 1
Kugel
  • 19,354
  • 16
  • 71
  • 103

5 Answers5

46

int[,] is a rectangular array - one object which has two dimensions. Each element of the array is an integer; all elements are stored contiguously in memory.

int[][] is a jagged array - an array where each element is in turn an int[]. (So it's an array of arrays.) Although each element of the "top level" array is stored contiguously, those elements are just references to other arrays, which could be anywhere in memory.

Whereas rectangular arrays always have the same number of columns per row, in a jagged array each element could have a different length (or indeed may be null).

Each has its own advantages and disadvantages; rectangular arrays are more compact in terms of memory, but don't allow sparse population. Jagged arrays are faster in the CLR, but don't have as good cache coherence. The extra space taken up by the "row arrays" in jagged arrays can be significant in some cases - if you have an int[10000, 2] that will only take up 80000 bytes plus the overhead of one array object, whereas in a jagged array it would be the 80000 bytes for the data and the overhead of 10001 array objects.

MSDN has more information in its arrays tutorial.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
11

The first is even (each row contains the same number of elements) The second is jagged (each row may contain a different number of elements)

To put it another way, the first one is a multi-dimensional array -- a rectangular array. The second one is an array of arrays (and hence, each individual array can be of a different length.)

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
6

int[x,y] is a two-dimensional array with fixed dimensions (e.g. int[4,5]).
int[][] is a jagged array, meaning it's an array of arrays, each of which can have a different number of elements.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
2

For creating a matrix, int[,] is easier. The jagged array int[][] takes more work (you need an extra loop to setup the complete matrix).

But you may still want to consider int[][] because it is faster.

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

As everyone else has mentioned, int[,] is a multidimensional/rectangular array, while int[][] is an array of arrays, or a "jagged" array.

As for when you'd use one over the other, int[][] can be a pain to set up (each row has to be created), but it seems to always be a bit faster. int[,]'s advantage is that it's simpler and the regularity is enforced, which makes it great for when you need that. (It's harder to accidentally replace a whole row in a 2D array than it can be with an array of arrays.)

cHao
  • 84,970
  • 20
  • 145
  • 172