• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please explain me Array Reference Assignments for Multidimensional Array

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic