Differences between Rectangular and Jagged Arrays

X

xllx.relient.xllx

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

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top