468,135 Members | 1,410 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,135 developers. It's quick & easy.

Differences between Rectangular and Jagged Arrays

Hi, I have two questions:

1.)Is it true that an rectangular array is really just an single
dimensional array that lets itself be treated as a multi-dimensional
array?

For example the following declaration:

int[,] rectangular = new int[5,10];

has it's memory layout in a sequential contiguous order, so, the above
is one big array consisting of 50 objects stored sequentially in
memory. Am I right?
2.) Jagged Arrays: These are truly, arrays of arrays unlike rectangular
arrays because; jagged arrays are not really stored in a sequential
order except for the first index; Example:

int[][] jagged = new int[5];

declares 5 sequential contiguous references in memory but the
sequential order stops at the following statement:

jagged[0] = new int[10];

the memory layout here is not sequential in order any more. correct?

So my conclusion is that this is the reason you have to type:

new int[] {1,2,3,4,5} ...

In a jagged initializer:

int[][] jagged = new int[][] {
new int[] {1,2,3,4,5},
new int[] {6,7,8,9,10}};

where in a rectangular array the expression:

new int [] {1,2,3,4,5} can be left out of the initializer:

int[,] rectangular = new int[,] {
{1,2,3,4,5},
{6,7,8,9,10}};

for a jagged array but not a rectangular array because a rectangular
array, unlike a jagged one, has it's objects stored in a sequential
order whereas jaggs do not, hence the need for new int[] {1,2,3,4,5}.

I might be coming a little confusing here to some of you but I hope I
got through what I'm trying to say and things like this really matter
to me (trust me) so I need to get this through my head before I move
ahead.

Thanks.

Jan 12 '06 #1
1 8656
I might be coming a little confusing here to some of you but I hope I
got through what I'm trying to say and things like this really matter
to me (trust me) so I need to get this through my head before I move
ahead.

Yes your understanding is basically correct.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nikhil Patel | last post: by
1 post views Thread by James dean | last post: by
3 posts views Thread by James dean | last post: by
1 post views Thread by Chris Wood | last post: by
3 posts views Thread by Ravi Singh (UCSD) | last post: by
2 posts views Thread by deko | last post: by
reply views Thread by =?Utf-8?B?TWljaGFlbA==?= | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.