aspose file tools*
The moose likes Beginning Java and the fly likes Multidimensional Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multidimensional Arrays" Watch "Multidimensional Arrays" New topic
Author

Multidimensional Arrays

Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
I understand how the Multidimensional Arrays are built.
My question is about this: Line 22
months = years[1];
What happens here?
What is the value of years[1]?
And any additional information that will help me understand multidimensional arrays
Thanks
public class ArrayStuff
{
public byte[] b;
public int i[];
public ArrayStuff[][] testme;
float years[][];
float months[];
ArrayStuff()
{
years = new float[3][4];
months = new float[3];
years[1][1] = 2000;
months[1] = 1;
months[2] = 2;
months[2] = years[1][3];
months[1] = years[2][2];

months = years[1];//I need help understanding



System.out.println(months[3]);
float f = 3;
months[(int)f] = 2;
}
public static void main(String args[])
{
ArrayStuff arrayStuff = new ArrayStuff();
System.out.println("Joe Sample " + arrayStuff.months[3]);
System.out.println("Smooth Jazz " + arrayStuff.years[1][3]);
}
}
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
years[1] contains a float array.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
I still am unclear on that. can you explain how that is possible. And where does the value of 3 come from.
Does that line create another element in the months array?
Thank you
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Jacob,
A multi-dimensional array is simply an array of arrays. So if I have a two-dimensional array, then all of the members of that array are one-dimensional (simple) arrays. If I have a three-dimensional array, then all of its members are two-dimensional. So in general, an array of N dimensions has for its elements arrays of N-1 dimensions for all N >= 1 assuming that an array of 0 dimensions is a simple element.
Hope this clears it up,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Yes that helps.
years = new float[3][4];
months = new float[3];
months = years[1];
float f = 3;
months[(int)f] = 2;
I just can't see how this adds aother element in the months[]. I was expecting an out of bounds error.
Not sure of the value that years[1]
Thank for the responses.
I am very impressed with this forum!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

years = new float[3][4];
months = new float[3];
months = years[1];
float f = 3;
months[(int)f] = 2;
I just can't see how this adds aother element in the months[]. I was expecting an out of bounds error.
Not sure of the value that years[1]

Arrays are stored in row major order. For your example the years[][] array consists of 3 simple arrays of 4 elements each. So when you set the months array equal to years[1], both months and years[1] now refer to the same array of four elements. Take a look at this code:

Here's the output:

Note that months[3] and years[1][3] now contain the float value of 2.0. That makes sense because they each refer to the same element.
Michael Morris
Jacob Michaels
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Thank you that helps!
I could actually say:
float[]months = years[0];
or
float[]months = years[2];
and it would mean the same thing.
Months would have 4 elements the same as the 3 simple arrays that years have.
I was reading that when you assign a two dimensional array:
example:
years[rows][columns];
that was confusing me with have 3 simple arrays with 4 elements.
Thank you for all for the reponses!
[ March 23, 2003: Message edited by: Jacob Michaels ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

I could actually say:
float[]months = years[0];
or
float[]months = years[2];
and it would mean the same thing.

Not exactly. Remember, that years[0] and years[2] are two distinct arrays. Also, you can have non-symettrical multi-dimensional arrays where the individual arrays are not all the same size. Consider this:

Here's the output:

Michael Morris
 
 
subject: Multidimensional Arrays