This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Swapping two object in array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Swapping two object in array" Watch "Swapping two object in array" New topic
Author

Swapping two object in array

zainu Mehmood
Ranch Hand

Joined: May 09, 2007
Posts: 45
how can i swap 2 object in an array,mind that original object not copy
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11448
    
  16

If i gave you two bucket of water, how would you swap the contents of them? If you can explain that process in words, you'll know what you need to do in your code.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
You can copy only references of the objects:
Here the objects will not be copied (only their references).
Is this what you are looking for?
Prosenjit Banerjee
Ranch Hand

Joined: Dec 18, 2002
Posts: 102
Can a method be written to swap two objects?
void swap(Object obj1, Object obj2)
{
???
}

And how about if we want to pass primitives?
void swap(int a, int b)
{
???
}

I don't think there is any way to do this. But how and why Java can be so cruel?


Always say the TRUTH only
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Prosenjit,
You are correct that you can't write a method to swap two objects or primitives. The references would only swap within the scope of the method; not the original variables.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
But how and why Java can be so cruel?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Sunali Anu
Greenhorn

Joined: Jun 04, 2007
Posts: 14
Use Collection.swap method to do the swapping... That works...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
Originally posted by Sunali Anu:
Use Collection.swap method to do the swapping... That works...


The task is to swap two array elements, not two collection elements. You don't mean to suggest converting the array to a List, then to call swap, and then to convert the List back to an array?
[ June 25, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
zainu Mehmood
Ranch Hand

Joined: May 09, 2007
Posts: 45
Ya Thanks God You have understnad my problem,because before your replys i think that i a duffer,but thanks to all that you solve my problem by saying that its not possible in java,but it should be,waiting for your reply,and Andris Jekabsons this is not what i want actully this the problem that i have, i want to swap original objects,andd Benerjee thanks that you understand my problem....waiting for your replys,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Thank you for the PM.

Andris Jekabsons has already told you what to do. You have to pass a (value of the) reference to the array, and the two indices you want to swap. The method you suggested in the thread transferred from the advanced forum, which is the same as Prosenjit Banerjee suggested, will not swap the original array, as Jeanne Boyarsky has already told you.

You have already had two satisfactory answers on your other thread.

I don't understand what you are saying about moving the internal details. Do you mean which memory location you are going to put the data in? You mean taking the contents of memory location 123456 and putting them in location 234567 and the contents of 234567 into 123456? A concept which has no place in a high-level language. Even pointers in C don't give that sort of control. It is far too error-prone. Also (Bruce Eckel, Thinking in Java, 4/e, Upper Saddle River: Prentice-Hall [2006] page 178ff) the garbage collector can move objects around in memory, so the reference which used to be 123456 can become 345678. In a high-level language you only want to deal with variable names, never with the memory locations.
zainu Mehmood
Ranch Hand

Joined: May 09, 2007
Posts: 45
Thanks Cambell,Actully you know what, one of my friend who is from .Net Lovers,he asked me this Question that is it possible but i was unable to answer him,and Now I understand that it is not possible to swap original objects in array in JAVA, you can only swap refrences....Ne ways thanks to all,and sorry if any one of you mind any thing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swapping two object in array