Differences between Rectangular and Jagged Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xllx.relient.xllx@gmail.com

    #1

    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.

  • Mattias Sjögren

    #2
    Re: Differences between Rectangular and Jagged Arrays

    [color=blue]
    >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.[/color]


    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.

    Comment

    Working...