How to Write a Basic Swap Function in Java

Is it possible to write swap method in Java?

Without using an array or objects, no, it is not possible to do it within a method.

Java method to swap primitives

You can't create a method swap, so that after calling swap(x,y) the values of x and y will be swapped. You could create such a method for mutable classes by swapping their contents¹, but this would not change their object identity and you could not define a general method for this.

You can however write a method that swaps two items in an array or list if that's what you want.

¹ For example you could create a swap method that takes two lists and after executing the method, list x will have the previous contents of list y and list y will have the previous contents of list x.

Function that swaps two integers

Short: You can't.

Long: You need some workaround, like wrapping them in a mutable datatype, e.g. array, e.g.:

public static void swap(int[] a, int[] b) {
int t = a[0]; a[0] = b[0]; b[0] = t;
}

but that doesn't have the same semantic, since what you're swapping is actually the array members, not the a and b themselves.

How to code a swap() method, or alternatives?

As you understand, the only way to swap to primitives by a method works only if they are in global scope or class scope.

As for arrays and objects, they are passed by reference in Java. So any changes that you make changes the actual argument to the method. On the other hand if you are looking for a neat way to swap two objects, just implement a swap method as a method in the class and call it like:

anObject.swap(anotherObject);

This way you can implement the swap however you want.

I want to swap two numbers by using another class with a swap method and not the general swap function which is commonly available

You are essentially swapping the two numbers twice, and therefore not swapping them at all:

swapper s = new swapper(a[j],a[j+1]); // this assigns a[j] to s.x and a[j+1] to s.y
s.swap(); // this swaps s.x and s.y
a[j] = s.y; // this assigns the original value of s.x (a[j]) to a[j]
a[j+1] = s.x; // this assigns the original value of s.y (a[j+1]) to a[j+1]

In order for the swapping to work as expected, change it to:

swapper s = new swapper(a[j],a[j+1]); 
s.swap();
a[j] = s.x;
a[j+1] = s.y;

Efficient swapping of elements of an array in Java

Nope. You could have a function to make it more concise each place you use it, but in the end, the work done would be the same (plus the overhead of the function call, until/unless HotSpot moved it inline — to help it with that, make the function static final).

Java: Why does this swap method not work?

This doesn't have anything to do with immutability of integers; it has to do with the fact that Java is Pass-by-Value, Dammit! (Not annoyed, just the title of the article :p )

To sum up: You can't really make a swap method in Java. You just have to do the swap yourself, wherever you need it; which is just three lines of code anyways, so shouldn't be that much of a problem :)

    Thing tmp = a;
a = b;
b = tmp;


Related Topics



Leave a reply



Submit