File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain me Array Reference Assignments for Multidimensional Array

 
Ami Ambre
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic