Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multidimensional Arrays

 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
years[1] contains a float array.
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic