my dog learned polymorphism*
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 Android Security Essentials Live Lessons 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[];

A) my=my[0];

B) my=my[0][0];

C) my[1]=my[1][2];

D) my[0][1]=c;

Dariusz Kordonski

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...

I am not understand array assignment from K&B Book on page 225
where they had given
B) legal
D) illegal
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I don't have the book with me, but please verify what you've posted, because none of these options are legal. And neither is the line...

In general, note that:
  • my references a 2-dimensional cat array.
  • my[x] references a 1-dimensional cat array.
  • my[x][y] references a cat.
  • c (apparently) references a 1-dimensional cat array.

  • [ August 09, 2008: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Okay, now I'm looking at the book.

    A, B, C, and D are all examples of "Illegal Array Reference Assignments," and underneath each one is an explanation of why they are illegal.

    Unfortunately, there might be some confusion, because to the right of these examples is a "key" for the diagram that's above this. The key is just showing that a solid arrow in the diagram represents a legal reference, and a dashed arrow represents an illegal reference. Because of the way they're positioned, it might look like the "legal" arrow corresponds to options A and B, and the "illegal" arrow corresponds to options C and D. But there is no relation. A, B, C, and D are all illegal.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Please explain me Array Reference Assignments for Multidimensional Array
     
    Similar Threads
    Arrays
    Object creation
    Array Creation
    two dimensional array
    Please explain me Array Reference Assignments for Multidimensional Array