Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please explain this code. How it works?


==========================================================



========================================================

Thanks in advance,
Mubeen Shaik.
 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
I also don't have any idea of how its working..
Help with answers please.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a1 refers to a 1 dimensional array, type A11[], of one component containing a reference of type A11 to the one and only instance of a class A11 object.

a2 is a 2 dimensional array, type A11[][], with two rows. Each row is that same one dimensional array referred to by a1.

a3 is a 3 dimensional array, type A11[][][], with 3 slices. Each slice is that same 2 dimensional array referred to by a2.

so a3's maximum subscripts are a3[2][1][0]


a2 actually refers to a 1 dimensional array of 2 references of type A11[].
Both references are set to the same value by copying the value of a1.

a3 actually refers to a 1 dimensional array of 3 references of type A11[][]. All 3 references are set to the same value by copying the value of a2.

The output is "A11".

[ December 15, 2004: Message edited by: Mike Gershman ]
[ December 15, 2004: Message edited by: Mike Gershman ]
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Thanks for the detailed explanation. But how the output is "A11".

Thanks in advance,
Mubeen Shaik
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mubeen, what's the type of the object that a3[2][1][0] refers to?
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry,

It refers to the type "class". Please correct me if i am wrong.

Thanks,
Mubeen Shaik
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was expecting you to answer "a3[2][1][0] refers to an object of type (or class) A11". So when you try to print such an object the class' toString() method would be called. And what does the toString() method do for class A11? It returns the string "A11".
 
Jugal Hans
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so when you try to print such an object the class' toString() method would be called.

why would the method be implicitly called ?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use an object reference "r" where a String is needed, "r" is treated like "r.toString()", which returns a String.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic