wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArrayList Hijinx and the S.O.P statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ArrayList Hijinx and the S.O.P statement" Watch "ArrayList Hijinx and the S.O.P statement" New topic
Author

ArrayList Hijinx and the S.O.P statement

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1




How does this output: [3, 7, 5, 9, 1]

I am not understanding how x and y refer to the same ArrayList object...

Does anyone have a simple explanation please?

dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
The List reference named x is passed into the doStuff method, and the result is assigned to the List reference named y. But notice that the doStuff method returns the same reference that was passed in. Drawing a picture can help you sort out this kind of puzzle very quickly.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Here is the picture:
1. z -> x
2. y-> z

Therefore y->x, where-> means refers to.

Hope this helps.
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1



Thanks all, my colleague helped explain some also...

I want to say again how much I appreciate everyone's responses and help.

I was completely baffled as to how the two collections could relate to each other, when there is not an explicit assignment that says x = y for instance. I think this concept is also something that the new Java programmer has to grasp and memorize like rules for a game like dungeons and dragons or something.

Respectfully,

TN
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayList Hijinx and the S.O.P statement