It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain me Array Reference Assignments for Multidimensional Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain me Array Reference Assignments for Multidimensional Array" Watch "Please explain me Array Reference Assignments for Multidimensional Array" New topic
Author

Please explain me Array Reference Assignments for Multidimensional Array

Ami Ambre
Ranch Hand

Joined: Dec 26, 2007
Posts: 58
cat [][]my= {{new cat("F"),new cat("Z")},
{new cat("B"),new cat("R"),new cat("L")}};

cat []c=new cat[];

my=my[0];
//can't assign a 1-D array to a 2-D array.

my=my[0][0];
//can't assign a nonarray object to a 2-D array reference.

my[1]=my[1][2];
//can't assign a nonarray object to a 1-D array reference.

my[0][1]=c;
//can't assign an array object to a nonarray reference my[0][1] can only refer to a cat object
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49


cat []c=new cat[]; // this won't work, you have to specify size of the instantiated array, e.g. new cat[5];

my=my[0];
//can't assign a 1-D array to a 2-D array.


my=my[0][0];
//can't assign a nonarray object to a 2-D array reference.

my[1]=my[1][2];
//can't assign a nonarray object to a 1-D array reference.

my[0][1]=c;
//can't assign an array object to a nonarray reference my[0][1] can only refer to a cat object[/QB]


What specifically you don't understand? There is one simple rule here: n-dimensional array reference of object x can only refer to instance of exactly n-dimensional array of object x (or subclass of x, since arrays are polymorphic) - period. In your case, cat[][] instance cannot be assigned to cat[] reference and vice versa, and so on...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19926
    
  43

Agreed. It looks like the comments does a very good job explaining everything. What is it that is not understood?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please explain me Array Reference Assignments for Multidimensional Array
 
It's not a secret anymore!