• Post Reply Bookmark Topic Watch Topic
  • New Topic

java, arrays, different sizes  RSS feed

 
M. Bogi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all,
hopefully somebody could help me out with (i assume ) trivial problem I am facing :
in the following code:



the program compiles but it doesnt run(obviously).
however, if i would change the array ba to:


the program doesnt even compile. Could you please explain why an 2D array works and why 3D array doesnt?

Thanks in advance
HK
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is no such thing as a 2D array; it is an array of arrays. Please show us the exact error message from the compiler. Are you trying to squeeze an array of arrays as an element of daa? That expects its elements to be arrays of arrays of arrays, or double[][][], so you cannot insert a double[][] there.

Those variable names are very poorly‑chosen and don't make the code at all easy to read. Nor does the lack of indentation.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain why an 2D array works and why 3D array doesnt?

Your original code has ba as a 3D "array" (actually, and array of arrays of arrays), and you say it compiles. But the above quote seems to contradict that. So which is it? And what is the error message you are getting when you say it does not compile?

Also you say the original code compiles but does not run. What do you mean by that? Does nothing happen? Does it print "This doesn't work"? Does your computer catch fire?
 
M. Bogi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,
i haven't formulated the question properly, sorry!(new on this forum )
what confused me is the following:

this code doesn't create the compiler error (i deleted sysout part)
double[][][][] daa=new double[3][][][];
double[][][] ba=new double[1][][];
daa[0]=ba;

However if i change it to
double[][][][] daa=new double[3][][][];
double[][] ba=new double[1][]; //this one ,why it doesnt compile?
daa[0]=ba;

i am getting following error:incompatible types: double[][] cannot be converted to double[][][]
daa[0]=ba;

basically i dont understand why in daa[0]  cannot insert ba  (as an array of an array).

Thanks in advance!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because all elements of a double[][][][] must be double[][][], so whenever you get to the rightmost [] in the complete array you have three pairs of [] to the left of you. If you insert that double[][] and go as far right as you can, you will only have two pairs of [] to your left.
Or: you must always have []s 4 deep, but inserting the double[][] where you tried to put it makes the array have only three []s.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!