Results 1 to 5 of 5
  1. #1
    Riaz Ali is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default How Multi-Dimensional arrays are represented in memory

    It is said that memory is single-dimensional, then how multi-dimensional arrays are represented (and manipulated) in memory. Please reply if anyone knows

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Riaz Ali View Post
    It is said that memory is single-dimensional, then how multi-dimensional arrays are represented (and manipulated) in memory. Please reply if anyone knows
    They say a picture says more than a thousand words, so here goes: given the following two dimensional array:

    Java Code:
    int[][] array= new int[3][4];
    ... the jvm builds it up as a bunch of one dimensional arrays:

    Java Code:
    +---+
    |   |   +---+---+---+---+
    |  ---> |   |   |   |   |
    |   |   +---+---+---+---+
    +---+
    |   |   +---+---+---+---+
    |  ---> |   |   |   |   |
    |   |   +---+---+---+---+
    +---+
    |   |   +---+---+---+---+
    |  ---> |   |   |   |   |
    |   |   +---+---+---+---+
    +---+
    As you can see the two dimensional array is just a bunch of one dimensional arrays held together by a so called 'dope vector' (the vertical array on the left). Each slot of that dope vector points to a row of the original array.

    kind regards,

    Jos
    Last edited by JosAH; 07-31-2010 at 09:58 PM.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Otherwise known as an array of arrays. The actual array variable is a handle to an array of other array handles, which are either handles to arrays of the objects or more array handles, and so on...

    Which means this is valid :)

    Java Code:
    +---+
    |   |   +---+---+---+---+
    |  ---> |   |   |   |   |
    |   |   +---+---+---+---+
    +---+
    |   |   +---+---+---+
    |  ---> |   |   |   |
    |   |   +---+---+---+
    +---+
    |   |   +---+---+---+---+
    |  ---> |   |   |   |   |
    |   |   +---+---+---+---+
    +---+
    Edit: can't create a 2D array like that using the simple Type[][] array = new Type[xSize][ySize] though. You'd need a for loop to do that.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default

    Quote Originally Posted by Singing Boyo View Post
    You'd need a for loop to do that.
    Not necessarily. There could just as well be individual assignment statements.

    db

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Not necessarily. There could just as well be individual assignment statements.
    Even a single initialization of the ragged array/matrix can do it:

    Java Code:
    int[][] array= { {1, 2, 3, 4}, {5, 6, 7}, {8, 9, 10 11}};
    kind regards,

    Jos

    edit: if you know that sizes and values of the individual arrays at compile time that is ...

Similar Threads

  1. Multi-dimensional arrays
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-15-2009, 08:50 AM
  2. [SOLVED] Multi-dimensional arrays
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2009, 02:52 PM
  3. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 03:34 PM
  4. Replies: 2
    Last Post: 01-07-2008, 07:45 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •