help with multidimensional arrays  
Author Message
cupidisdangerous





PostPosted: 2005-6-5 2:49:00 Top

java-programmer, help with multidimensional arrays Hi
I am a newbie in the world of Java programming. I have recently started
learning the language. Apart from other hickups one which has been on
my nerves is the concept of multidimensional arrays. As far as my
understanding goes these are array of arrays.

1. WHAT DOES THIS MEAN?

2. ARE ARRAYS RESTRICTED TO THREE DIMENSIONS ONLY OR THERE IS ANY OTHER
LIMIT AND HOW DOES THREE DIMENSIONAL OR HIGHER DIM ARRAYS WORK?

Does two dim arrays represent a pigeon hole type thing in which you
find a particular hole by giving row and column address e,g. box
located at row 5 and column 2.

3. IF THIS CONCEPT OF TWO DIMENSIONAL ARRAYS IS CORRECT THEN HOW DOES
THREE DIMENSIONAL OR HIGHER DIM ARRAYS WORK.

Presently I am going through a book written by Herber Schildt known as
Java 2 complete reference, Following is a program which demonstrates
the two dim arrays. I am unable to understand the make of the program
may be so many loops are troubling me. I am especially confused about
the functioning of the question mark steps please help me out there.

// Demonstration of concept of two dimensional arrays

class twoD {
public static void main (String args [ ]) {
int [ ][ ] twoD = new int [4][5];
int i,j,k = 0;
for (i=0; i<4; i++)
for (j=0; j<5; j++) {
twoD [i][j] = k; ?
k++; ?
}
for (i=0; i<4; i++) {
for (j=0; j<5; j++)
System.out.println (twoD[i][j] + " ");
System.out.println();
}
}
}

4. HOW AND WHY SO MANY LOOPS ARE FUNCTIONING TOGETHER?
(While replying please be patient and keep in mind that u r talking to
an absolute beginner in the world of Java programming)

 
Bjorn Abelli





PostPosted: 2005-6-5 3:21:00 Top

java-programmer >> help with multidimensional arrays
<email***@***.com> wrote...

[on multidimensional arrays]

> As far as my
> understanding goes these are array of arrays.

To start with, it's just a bit annoying when you shout (writing with all
caps).

> 1. WHAT DOES THIS MEAN?

It means that an array can have only *one* dimension. To get
"two-dimensional arrays" you really have one array, where the *elements* of
that array are arrays in turn.

> 2. ARE ARRAYS RESTRICTED TO THREE DIMENSIONS ONLY OR
> THERE IS ANY OTHER LIMIT AND HOW DOES THREE DIMENSIONAL
> OR HIGHER DIM ARRAYS WORK?

Just as a "two-dimensional array" is an array where the elements are arrays,
these arrays can contain elements of array-type, etc, etc...

So there are no limits to how many dimensions you can have when you're using
arrays of arrays, than what other limits the machine have that you're
running the program on...

> Does two dim arrays represent a pigeon hole type thing
> in which you find a particular hole by giving row and
> column address e,g. box located at row 5 and column 2.

If you choose to call it "rows" and "columns" is up to you, as the
addressing of each element in a multidimensional array can be many (one
practice when naming the first two dimension as "rows" and "columns", is to
name the third dimension "depth", the fourth "volume", etc...)

int a = twoD[5][2];

> 3. IF THIS CONCEPT OF TWO DIMENSIONAL ARRAYS IS CORRECT
> THEN HOW DOES THREE DIMENSIONAL OR HIGHER DIM ARRAYS WORK.

Just add a "dimension" on creation...

int[][][] threeD = new int[4][5][6];

...fill it, and then you can address it with "row", "column" and "depth".

int b = threeD[5][2][3];

> Presently I am going through a book written by Herber Schildt known as
> Java 2 complete reference, Following is a program which demonstrates
> the two dim arrays. I am unable to understand the make of the program
> may be so many loops are troubling me. I am especially confused about
> the functioning of the question mark steps please help me out there.

In your example, there's one construction to fill the array, and one to read
from it and print the results.

As the arrays have 2 dimensions, each of those constructions has one outer
loop (for the "rows") and one inner loop (for the "columns").

That makes 2 x 2 iterations (filling/reading x rows/colums).

If you have more dimensions, you have to add further "inner" loops for
those.

> // Demonstration of concept of two dimensional arrays
>
> class twoD {
> public static void main (String args [ ]) {
> int [ ][ ] twoD = new int [4][5];

// As the indexing of arrays starts at 0 (zero)
// the indexers are assigned those values to
// start with

> int i,j,k = 0;

// The outer loop increments the "row index" i with 1
// for each turn

> for (i=0; i<4; i++)

// The inner loop increments the "column index" j with 1
// for each turn

> for (j=0; j<5; j++) {

// This line assigns the value of k, to the element
// addressed by "row" i and "column" j.

> twoD [i][j] = k; ?

// This line increments k with 1 for each turn

> k++; ?

> }
> for (i=0; i<4; i++) {
> for (j=0; j<5; j++)
> System.out.println (twoD[i][j] + " ");
> System.out.println();
> }
> }
> }
>
> 4. HOW AND WHY SO MANY LOOPS ARE FUNCTIONING TOGETHER?
> (While replying please be patient and keep in mind that
> u r talking to an absolute beginner in the world of Java
> programming)

See comments inline above.

// Bjorn A


 
cupidisdangerous





PostPosted: 2005-6-5 13:33:00 Top

java-programmer >> help with multidimensional arrays I am so thankful to u for ur patience and time. I also appologize for
"shouting". So if i am not wrong then multidimensional arrays are
actually the depth levels i mean if i say that i have created a five
dimensional array then it would mean that the depth of that arrays will
be of five levels.
1
2
3
4
5

regarding the statement ( twoD [i][j] = k ) I understand that it is
actually assigning a value to the element of array twoD located at row
i and column j and at the next step with ( k++ ) we r incrementing the
value and storing at at certain other location.

Thanx alot again and if my concept still needs refinement please do let
me know and if you have a reference of some good book, ebook or website
please do refer it to me.