• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Reference

 
John Wolf
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone please give me an example of what is being implied here,

"If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to." - K&B

Regards,
Sumit
[ May 03, 2005: Message edited by: Sumit Sadana ]
 
S.L.Narayanan
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumit

This is very simple. Consider the following example.

int a1[];
int a2[][];
int a3[][][];


a2=new int[3][2];

now, a1 is a 1 dimensional array, a2 is a 2 dimensional array and a3 is a 3 dimensional array.

Now a1 as not defined. Can u assign a2 to a1 like a1=a2;???

No.. You cant because they are of difft dimensions. But you can assign
a1=a2[0]; because a2[0] is a 1 dimensional array of size 2.

Similarly,
you cannot assign a3=a2; but you can do as
a3=new int[3][][]; and a3[0]=a2;


Clear???
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just another piece of fallacy that many technical books portray.
Any suggestions that refer to 2 (or more) dimensional arrays should be ignored, since multi-dimensional arrays are not supported in Java and heeding such advice will lead to further confusion on top of the already existing confusion. This is what I assume your book is erroneously attempting to communicate.

The real point is that a reference whose type is array, cannot be assigned to another reference of a different type (except for java.lang.Object, java.lang.Cloneable or java.io.Serializable with an explicit cast, but that is irrelevant). By no strange coincidence, a reference of type (for example) int[] (array of int) is not of the same type as a reference of type int[][] (array of array of int) - the distinction is obvious, is it not?

I sometimes think that this forum goes in cycles of questions, with the same question being asked at certain time intervals.
I'll say it one more time:
Java does not have support for multi-dimensional arrays
(and the argument that the JLS quotes the use of multi-dimensional arrays holds no water).
http://qa.jtiger.org/GetQAndA.action?qids=63
 
John Wolf
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Guys! This is makes lot more sense now.

Regards,
Sumit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic