• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"copy of refrence "issue

 
zainu Mehmood
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duplicate question, too. Almost identical question asked very recently by same poster.

Suggest we all move on.
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, if you carefully read the original post like I did, you might try this instead:



 
zainu Mehmood
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic