GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""copy of refrence "issue" Watch ""copy of refrence "issue" New topic
Author

"copy of refrence "issue

zainu Mehmood
Ranch Hand

Joined: May 09, 2007
Posts: 45
how can i swap 2 object in an array,mind that original objects not copy of refrences,i have a swap function in which i take two argument mean two diffrent objects,but as you know in java there is copy of refrence concept when passing arguments,so when i swap it only swap's copy of that refrence not the orginal one......so what should i do,any idea
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
More of a beginner's question. Please post in the appropriate forum, where you will be told such a swap function never works the way you are asking it to.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Duplicate question, too. Almost identical question asked very recently by same poster.

Suggest we all move on.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
You should be able to do it if you pass in a reference to the array itself. This swap function simply swaps the first two elements of the array:



You'll probably want to also pass in the indices that you want to swap. In that case you'll have to write your own error avoidance code.

This works because I'm not trying to change the reference that was passed in to the swap function, I'm changing its internal data.
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Or, if you carefully read the original post like I did, you might try this instead:



zainu Mehmood
Ranch Hand

Joined: May 09, 2007
Posts: 45
Thanks Bill, i have done this before posting this Thread,But i want to swap original refrences not their internal data,but i am happy that you understand my question
 
GeeCON Prague 2014
 
subject: "copy of refrence "issue