Forums Register Login

Please explain me Array Reference Assignments for Multidimensional Array

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
    +Pie Number of slices to send: Send
    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.
    This tiny ad is wafer thin:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 656 times.
    Similar Threads
    Arrays
    two dimensional array
    Array Creation
    Please explain me Array Reference Assignments for Multidimensional Array
    Object creation
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 00:51:19.