wood burning stoves 2.0*
The moose likes Java in General and the fly likes swapping the data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "swapping the data" Watch "swapping the data" New topic
Author

swapping the data

MD SALAHUDDIN
Greenhorn

Joined: Oct 04, 2003
Posts: 7
I have some values in array like
int a[]={1,2,3,4,5,6,7,8,.....};
i have to swap the elements of array to get the result
a[]={2,1,4,3,6,5,8,7,...};
can anyone help me out.
Thanks in advance
sallu
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
What specific problems are you having ?
I'm not going to do your homework for you
John Brookes
Greenhorn

Joined: Feb 26, 2004
Posts: 29
If you just have to swap each pair within the array, you'd need a dummy variable and a loop.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
To illustrate that:

There are some cool mathematical tricks to swap two integers without using a temp variable, but this is the kindest solution to future readers of your code.
Hope that helped. And hope I haven't just stepped in and done somebody's homework.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: swapping the data
 
Similar Threads
Dynamic Property Strings
using sin in java
how to write constructor?
simple sorting algorithm
Two Dimensional Array in struts