Jagged arrays

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hello,

So I was reading this C# book and we have got to jagged arrays. It seems that they are used when there is no guarantee of length from the array. But what I don't understand is how they are declared and how they are used.

The book shows this as a bad example:

C#:
 jaggedIntArray = new int[3][4];

But what I don't understand is WHY would that be bad. He explains that it wouldn't be useful to use this because you can get to the same result by declaring a multidimensional array. But why? I am a little confused here.

Thanks
 
A multidimensional array is a matrix, i.e. a single object with, in the case of a 2D array, rows and columns that form a rectangle and each intersection contains an element. A jagged array is not a matrix. It is a 1D array of 1D arrays, so multiple objects. The whole point of a jagged array is that it CAN be jagged. In your example, you would be specifying that each inner array is the same size so there is no jaggedness so there's no obvious reason to use a jagged array over a 2D array. If your data is such that each "row" WILL be or MIGHT be a different size then you should use a jagged array. If every "row" WILL be the same size then you'd need some other compelling reason to use a jagged array over a 2D array because the 2D array is more efficient otherwise.
 
Oh right I see. Thanks for the explanation. The book I am following is the one you suggested, but sometimes the learning curve gets very steep and it can take some explanation to understand what is going on.

Thanks for the reply.
 
Back
Top Bottom