+ 1
Why do we need Jagged Arrays? Can't we do the same thing with Multidimensional arrays? It is so confusing to guess when to use w
2 Answers
+ 1
Each row of any multidimensional array will have fixed length. In case of Jagged arrays, each row can have different number of elements.
Example: User enters an array { 3,4,5,6,8,9,10,12,15,18}
and we want to store them in different arrays as (n%3)
Then its better to use jagged array as this will save memory.
Jagged array will look like
{ { 3,6,9,12,15,18}, {4,10}, {5,8} }
Jagged arrays can also be used for hashing.
0
the most common use of jagged arrays is for efficiency - if many of your "rows" (in a 2 column array) will be empty, you can save a great deal of memory by not initialising them. for "jagged" think "sparse". the ones you use may well all be the same size, but you don't use all of them.