Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between int[][] and int[,]? [duplicate]

Tags:

c#

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?

like image 999
Kugel Avatar asked Aug 12 '10 14:08

Kugel


People also ask

Is there any difference between int [] A and int a []?

What is the difference between int[] a and int a[] in Java? There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.

What is difference between int and double?

int is a 32 bit data type which can be used to store integer. double is 64 bit data type used to store floating point numbers that is numbers which has something after decimal point.

What is the difference between int and int [] in Java?

Sr. No. A int is a data type that stores 32 bit signed two's compliment integer. On other hand Integer is a wrapper class which wraps a primitive type int into an object.

Is int * and int * the same?

int * means a pointer to an integer in your memory. The [] bracket stands for an array. int a[10]; would make an array of 10 integers. int *a; would make a pointer to an integer.


2 Answers

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.

like image 75
Jon Skeet Avatar answered Sep 18 '22 07:09

Jon Skeet


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.)

like image 23
Kirk Woll Avatar answered Sep 19 '22 07:09

Kirk Woll