aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
Hi All,

Please explain this code. How it works?


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



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

Thanks in advance,
Mubeen Shaik.


Sun Certified Java Programmer
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
Hi..
I also don't have any idea of how its working..
Help with answers please.


SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
Mike,

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

Thanks in advance,
Mubeen Shaik
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Mubeen, what's the type of the object that a3[2][1][0] refers to?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
Barry,

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

Thanks,
Mubeen Shaik
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 30, 2004
Posts: 17
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

Joined: Mar 13, 2004
Posts: 1272
When you use an object reference "r" where a String is needed, "r" is treated like "r.toString()", which returns a String.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays