wood burning stoves*
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 Android Security Essentials Live Lessons this week in the Android 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

ArrayList Hijinx and the S.O.P statement

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192

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

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.


jQuery in Action, 2nd edition
subject: ArrayList Hijinx and the S.O.P statement
Similar Threads
S&B Practice Exams, Test 1, # 14 question
Fellows, please come in and check this question
OCP Practice Exams Sef-Assessment Test 1 Q14
Collections.copy method for lists ? how it works