Multidimensional arrays in Java.

Discussion in 'Java' started by getsanjay.sharma@gmail.com, Jun 10, 2007.

  1. Guest

    Hello to all programmers out there.

    I have a simple query regarding multidimensional arrays.

    When we create object references in function definitions, they are
    created on the stack. Java doesn't support multidimensional arrays
    directly. We have to create an array of array to support this
    functionality. For eg.

    Student[][] stud = new Student[3][3];

    Here the stud reference variable or object reference is created on the
    stack. But stud here indirectly points to an anonymous array which
    again contains three elements whose contents are of type reference to
    Student instance. So are the anonymous array references created on
    stack or on heap?

    What does one mean when he says 'Arrays in Java are different than
    those in other languages like C or C++? Is it because they are objects
    in Java while not in C++?

    Thanks and regards,
    S T S
     
    , Jun 10, 2007
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > Hello to all programmers out there.
    >
    > I have a simple query regarding multidimensional arrays.
    >
    > When we create object references in function definitions, they are
    > created on the stack. Java doesn't support multidimensional arrays
    > directly. We have to create an array of array to support this
    > functionality. For eg.
    >
    > Student[][] stud = new Student[3][3];
    >
    > Here the stud reference variable or object reference is created on the
    > stack. But stud here indirectly points to an anonymous array which
    > again contains three elements whose contents are of type reference to
    > Student instance. So are the anonymous array references created on
    > stack or on heap?


    If the line above appears inside a method or constructor
    or initialization block, the reference variable `stud' is
    local to that block: "on the stack," in your terms. `stud'
    refers to an array of three elements, each being a reference
    to a one-dimensional array of Student. This array, like all
    objects of all kinds, is "on the heap." The three references
    in this array point to three arrays containing references to
    Student objects. The first-level array, all three second-
    level arrays, and any Student objects the second-level array
    may eventually refer to are "on the heap."

    stud: *---> [ * * * ]
    | | |
    | | +---> [ * * * ]
    | | | | |
    | | v v v
    | | (S)(S)(S)
    | |
    | +---> [ * * * ]
    | | | |
    | v v v
    | (S)(S)(S)
    |
    +---> [ * * * ]
    | | |
    v v v
    (S)(S)(S)

    Your use of the word "anonymous" makes me suspect you do
    not quite understand the difference between a reference variable
    like `stud' and the thing it refers to. `stud' is not the name
    of an array; it is the name of a reference variable. Arrays do
    not have "names" at all; all Java objects are "anonymous."

    > What does one mean when he says 'Arrays in Java are different than
    > those in other languages like C or C++? Is it because they are objects
    > in Java while not in C++?


    Since I am not the "one" who said this, I can only guess
    what the sayer meant. There are several differences between
    Java arrays and C arrays (I can't speak for C++), one of them
    being that a Java array is a full-fledged Java object. C arrays
    are not objects in the Java sense -- they are objects in the C
    sense, but the C Standard uses the word "object" with a meaning
    that doesn't match Java's.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 10, 2007
    #2
    1. Advertising

  3. Alex Kizub Guest

    >
    > Your use of the word "anonymous" makes me suspect you do
    > not quite understand the difference between a reference variable
    > like `stud' and the thing it refers to. `stud' is not the name
    > of an array; it is the name of a reference variable. Arrays do
    > not have "names" at all; all Java objects are "anonymous."
    >
    > > What does one mean when he says 'Arrays in Java are different than
    > > those in other languages like C or C++? Is it because they are objects
    > > in Java while not in C++?

    >
    > Since I am not the "one" who said this, I can only guess
    > what the sayer meant. There are several differences between
    > Java arrays and C arrays (I can't speak for C++), one of them
    > being that a Java array is a full-fledged Java object. C arrays
    > are not objects in the Java sense -- they are objects in the C
    > sense, but the C Standard uses the word "object" with a meaning
    > that doesn't match Java's.
    >
    > --
    > Eric Sosman
    >


    In C++ array is always (physically) one dimension array which could be
    translated to many dimensions. For example:
    [ 1, 2, 3, 4, 5, 6, 7, 8 ] could be reffered as int[8] or int[2][4] or
    even int[2][2][2].
    All elements are similar type.

    In Java only lower level is this type when all other are arrays!
    In your example stud[1][1] is the Student but stud[1] is array of
    Students.
    Which brings interesting idea/implementation of varydimensioanl
    arrays. Watch this and tell me how you can do this in C++?

    stud: *---> [ * * * ]
    | | |
    | | +---> [ * ]
    | | |
    | | v
    | | (S)
    | |
    | +---> [ * * * ]
    | | | |
    | v v v
    | (S)(S)(S)
    |
    +---> [ * * * * * * * * * ]
    | | | null | | | |
    v v v v v v v
    (S)(S)(S) (S)(S)(S)(S)
    It's possible because stud[][] is array of array and each array could
    be different.

    Alex.
    Visit http://symade.org/ and http://symade.com/
     
    Alex Kizub, Jun 11, 2007
    #3
  4. Alex Kizub wrote:
    > In C++ array is always (physically) one dimension array which could be
    > translated to many dimensions. For example:
    > [ 1, 2, 3, 4, 5, 6, 7, 8 ] could be reffered as int[8] or int[2][4] or
    > even int[2][2][2].
    > All elements are similar type.


    I don't like the phrase "is always (physically) one dimension array".

    They are not. But all array elements are always stored contiguous.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jun 11, 2007
    #4
  5. Eric Sosman Guest

    [OT] Re: Multidimensional arrays in Java.

    Alex Kizub wrote:
    >
    > In C++ array is always (physically) one dimension array which could be
    > translated to many dimensions. For example:
    > [ 1, 2, 3, 4, 5, 6, 7, 8 ] could be reffered as int[8] or int[2][4] or
    > even int[2][2][2].


    Surprising; I'd imagined that C++ was similar to C insofar
    as "shared concepts" are concerned, but from what you say it
    must be quite different.

    > All elements are similar type.
    >
    > In Java only lower level is this type when all other are arrays!
    > In your example stud[1][1] is the Student but stud[1] is array of
    > Students.
    > Which brings interesting idea/implementation of varydimensioanl
    > arrays. Watch this and tell me how you can do this in C++?
    >
    > stud: *---> [ * * * ]
    > | | |
    > | | +---> [ * ]
    > | | |
    > | | v
    > | | (S)
    > | |
    > | +---> [ * * * ]
    > | | | |
    > | v v v
    > | (S)(S)(S)
    > |
    > +---> [ * * * * * * * * * ]
    > | | | null | | | |
    > v v v v v v v
    > (S)(S)(S) (S)(S)(S)(S)
    > It's possible because stud[][] is array of array and each array could
    > be different.


    I cannot answer for C++ because I don't know the language,
    and from what you said earlier it must be very different from C.
    But in C, this outline is easy to achieve, and in fact the
    resulting structure has some similarities to Java's arrays:

    /* C source (no error checking) */
    Student **array;
    array = malloc(3 * sizeof *array);
    array[0] = malloc(9 * sizeof *array[0]);
    array[0][0] = newStudent();
    array[0][1] = newStudent();
    array[0][2] = newStudent();
    array[0][3] = NULL;
    array[0][4] = NULL;
    array[0][5] = newStudent();
    array[0][6] = newStudent();
    array[0][7] = newStudent();
    array[0][8] = newStudent();
    array[1] = malloc(3 * sizeof *array[1]);
    array[1][0] = newStudent();
    array[1][1] = newStudent();
    array[1][2] = newStudent();
    array[2] = malloc(1 * sizeof *array[2]);
    array[2][0] = newStudent();

    The above is approximately equivalent to

    // Java source (could be shortened)
    Student[][] array = new Student[3][];
    array[0] = new Student[9];
    array[0][0] = new Student();
    array[0][1] = new Student();
    array[0][2] = new Student();
    // array[0][3] and array[0][4] already == null
    array[0][5] = new Student();
    array[0][6] = new Student();
    array[0][7] = new Student();
    array[0][8] = new Student();
    array[1] = new Student[3];
    array[1][0] = new Student();
    array[1][1] = new Student();
    array[1][2] = new Student();
    array[2] = new Student[1];
    array[2][0] = new Student();

    .... where "approximately" covers a multitude of sins.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 11, 2007
    #5
  6. Re: [OT] Re: Multidimensional arrays in Java.

    Eric Sosman wrote:
    > Alex Kizub wrote:
    >> In C++ array is always (physically) one dimension array which could be
    >> translated to many dimensions. For example:
    >> [ 1, 2, 3, 4, 5, 6, 7, 8 ] could be reffered as int[8] or int[2][4] or
    >> even int[2][2][2].

    >
    > Surprising; I'd imagined that C++ was similar to C insofar
    > as "shared concepts" are concerned, but from what you say it
    > must be quite different.


    Arrays in C and C++ are the same.

    > I cannot answer for C++ because I don't know the language,
    > and from what you said earlier it must be very different from C.
    > But in C, this outline is easy to achieve, and in fact the
    > resulting structure has some similarities to Java's arrays:
    >
    > /* C source (no error checking) */
    > Student **array;
    > array = malloc(3 * sizeof *array);
    > array[0] = malloc(9 * sizeof *array[0]);
    > array[0][0] = newStudent();


    The above is an array of arrays or pointer to pointers
    like a Java 2D array.

    It works fine in C++ also (you would use new instead of
    malloc, but that is a detail).

    But besides that C/C++ has multi dimensional arrays.

    And C# also has:
    int[][] a;
    int[,] b;
    for the two different approaches to multi dimensional
    arrays.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jun 11, 2007
    #6
  7. Guest

    On Jun 11, 12:44 am, Eric Sosman <> wrote:
    > wrote:
    > > Hello to all programmers out there.

    >
    > > I have a simple query regarding multidimensional arrays.

    >
    > > When we create object references in function definitions, they are
    > > created on the stack. Java doesn't support multidimensional arrays
    > > directly. We have to create an array of array to support this
    > > functionality. For eg.

    >
    > > Student[][] stud = new Student[3][3];

    >
    > > Here the stud reference variable or object reference is created on the
    > > stack. But stud here indirectly points to an anonymous array which
    > > again contains three elements whose contents are of type reference to
    > > Student instance. So are the anonymous array references created on
    > > stack or on heap?

    >
    > If the line above appears inside a method or constructor
    > or initialization block, the reference variable `stud' is
    > local to that block: "on the stack," in your terms. `stud'
    > refers to an array of three elements, each being a reference
    > to a one-dimensional array of Student. This array, like all
    > objects of all kinds, is "on the heap." The three references
    > in this array point to three arrays containing references to
    > Student objects. The first-level array, all three second-
    > level arrays, and any Student objects the second-level array
    > may eventually refer to are "on the heap."
    >
    > > What does one mean when he says 'Arrays in Java are different than
    > > those in other languages like C or C++? Is it because they are objects
    > > in Java while not in C++?

    >
    > Since I am not the "one" who said this, I can only guess
    > what the sayer meant. There are several differences between
    > Java arrays and C arrays (I can't speak for C++), one of them
    > being that a Java array is a full-fledged Java object. C arrays
    > are not objects in the Java sense -- they are objects in the C
    > sense, but the C Standard uses the word "object" with a meaning
    > that doesn't match Java's.
    >
    > --
    > Eric Sosman
    >


    Thanks to all of you for your detailed and really good
    illustrations. So I guess it stands that only the parent reference for
    a local multidimensional array resides on the stack. All other things
    including the data along with references of other arrays lie on the
    heap.

    > Your use of the word "anonymous" makes me suspect you do
    > not quite understand the difference between a reference variable
    > like `stud' and the thing it refers to. `stud' is not the name
    > of an array; it is the name of a reference variable. Arrays do
    > not have "names" at all; all Java objects are "anonymous."

    Sorry my bad. By anonymous I meant that the multidimensional array
    reference has a name but its inner array references which it holds
    have no distinct name and are referred by stud[0], stud[1] and so on.

    So for primitive types the illustration would be something like?

    <---stack----> <-------heap---------------->

    primMultiArr:* ---> [ * * * ]
    | | |
    | | +---> [ 2 2 2 ]
    | |
    | +---> [ 3 3 3 ]
    |
    +---> [ 4 4 4 ]

    Thanks and regards,
    S T S
     
    , Jun 12, 2007
    #7
  8. Eric Sosman Guest

    wrote On 06/12/07 12:09,:
    > On Jun 11, 12:44 am, Eric Sosman <> wrote:
    >> [...]
    >> Your use of the word "anonymous" makes me suspect you do
    >>not quite understand the difference between a reference variable
    >>like `stud' and the thing it refers to. `stud' is not the name
    >>of an array; it is the name of a reference variable. Arrays do
    >>not have "names" at all; all Java objects are "anonymous."

    >
    > Sorry my bad. By anonymous I meant that the multidimensional array
    > reference has a name but its inner array references which it holds
    > have no distinct name and are referred by stud[0], stud[1] and so on.


    Your understanding may be right, but I'm still not
    quite sure. Let's try it another way:

    int[] blob = new int[42];

    In this snippet, `blob' is the name of a reference variable,
    and the reference variable refers to a nameless array object
    containing forty-two `int' elements. We sometimes use the
    name `blob' as if it named the array rather than the array
    reference: We say "The values in `blob' are all non-negative"
    or "There are no duplicate values in `blob'" or things of
    that sort. But that's just a kind of shorthand, a slightly
    sloppy but convenient way of speaking. (I don't disapprove
    of the sloppy usage; it's certainly better than saying "The
    elements of the nameless array referred to by `blob' are
    sorted in non-decreasing order!")

    Sometimes, though, we need to be careful about what's
    what. Every week, it seems, somebody continues the above
    snippet with something like

    int[] glob = blob;
    glob[0] += 1;

    .... and then wonders why the value of blob[0] has changed:
    he only changed glob[0], right? This illustrates a confusion
    between the reference variables `blob' and `glob' and the
    nameless array they both refer to.

    A couple more points to help cement the idea that `blob'
    is not the "name" of the array:

    - Given the snippet and its continuation, why is the
    array named `blob' and not `glob'?

    - If the array is named `blob', what happens to its
    name after `blob = null'? The array still exists
    and is not garbage (`glob' refers to it), but are
    you going to say its name has changed?

    - In something like

    int[] cat = new int[42];
    int[] dog = new int[17];
    if (moon.phase() == Moon.FULL) {
    int[] tmp = cat;
    cat = dog;
    dog = tmp;
    }

    ... what is the "name" of the 42-element array?

    > So for primitive types the illustration would be something like?
    >
    > <---stack----> <-------heap---------------->
    >
    > primMultiArr:* ---> [ * * * ]
    > | | |
    > | | +---> [ 2 2 2 ]
    > | |
    > | +---> [ 3 3 3 ]
    > |
    > +---> [ 4 4 4 ]


    Yes; this would correspond to something like

    void method() {
    int[][] primMultiArr = new int[][] {
    { 2, 2, 2 },
    { 3, 3, 3 },
    { 4, 4, 4 },
    };
    ...
    }

    --
     
    Eric Sosman, Jun 12, 2007
    #8
  9. Guest

    "I now understand that objects in Java are inherently 'nameless'. We
    make a mistake in assuming that the reference variable which points to
    it is the name of that object. Array references which are members of
    objects always lie on heap since the object itself lies on the heap."

    Thanks a lot `Eric' for such a good explanation. Your concepts are
    flawless. Will keep posting in this wonderful newsgroup.
     
    , Jun 12, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. d[ - - ]b

    Multidimensional arrays? anything else?

    d[ - - ]b, May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,681
    AlexS
    May 18, 2004
  2. Jay
    Replies:
    1
    Views:
    2,606
    BarryNL
    Jan 30, 2004
  3. geclinke
    Replies:
    1
    Views:
    5,120
    jackie
    Jun 18, 2004
  4. Philipp
    Replies:
    21
    Views:
    1,174
    Philipp
    Jan 20, 2009
  5. Francesco
    Replies:
    2
    Views:
    1,151
    Francesco
    Nov 6, 2009
Loading...

Share This Page