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.
How to write a swap method that takes an array and two integers
static <T> void swap(T[] array, int a, int b){
T temp = array[a];
array[a] = array[b];
array[b] = temp;
}
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.
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.
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;
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
).
Related Topics
Java 9, Compatability Issue with Classloader.Getsystemclassloader
Java Serialization - Java.Io.Invalidclassexception Local Class Incompatible
Observer Is Deprecated in Java 9. What Should We Use Instead of It
Check Is a Point (X,Y) Is Between Two Points Drawn on a Straight Line
How to Pass Parameter to Jsp:Include via C:Set? What Are the Scopes of the Variables in Jsp
Jcomponent Stops Getting Rendered Once It Goes Off the Screen
Is It Possible with Spring Boot to Serve Up Jsps with a Jar Packaging
Splitting a CSV File with Quotes as Text-Delimiter Using String.Split()
Self Signed X509 Certificate with Bouncy Castle in Java
Custom Objectmapper with Jersey 2.2 and Jackson 2.1
How to Get a Client's MAC Address from Httpservlet
Exact Difference Between Charsequence and String in Java
Java: How to Do Double-Buffering in Swing