Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Multidimensional arrays in Java.

Reply
Thread Tools

Multidimensional arrays in Java.

 
 
getsanjay.sharma@gmail.com
Guest
Posts: n/a
 
      06-10-2007
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-10-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Alex Kizub
Guest
Posts: n/a
 
      06-11-2007
>
> 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
> (E-Mail Removed)


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/

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      06-11-2007
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-11-2007
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
(E-Mail Removed)lid
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      06-11-2007
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
 
Reply With Quote
 
getsanjay.sharma@gmail.com
Guest
Posts: n/a
 
      06-12-2007
On Jun 11, 12:44 am, Eric Sosman <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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
> (E-Mail Removed)


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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-12-2007
(E-Mail Removed) wrote On 06/12/07 12:09,:
> On Jun 11, 12:44 am, Eric Sosman <(E-Mail Removed)> 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 },
};
...
}

--
(E-Mail Removed)
 
Reply With Quote
 
getsanjay.sharma@gmail.com
Guest
Posts: n/a
 
      06-12-2007
"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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting pointers/arrays to multidimensional arrays Francesco C++ 2 11-06-2009 09:04 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Multidimensional arrays in Struts form? geclinke Java 1 06-18-2004 03:31 PM
Multidimensional arrays? anything else? d[ - - ]b ASP .Net 2 05-18-2004 12:17 PM
Cast to multidimensional arrays Jay Java 1 01-30-2004 04:27 PM



Advertisments