How to write a basic swap function in Java [duplicate]
Here is one trick: public static int getItself(int itself, int dummy) { return itself; } public static void main(String[] args) { int a = 10; int b = 20; a = getItself(b, b = a); }
Here is one trick: public static int getItself(int itself, int dummy) { return itself; } public static void main(String[] args) { int a = 10; int b = 20; a = getItself(b, b = a); }
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).
C# 7 introduced tuples which enables swapping two variables without a temporary one: int a = 10; int b = 2; (a, b) = (b, a); This assigns b to a and a to b.
Here’s a one-liner to swap the values of two variables. Given variables a and b: b = [a, a = b][0]; Demonstration below: var a=1, b=2, output=document.getElementById(‘output’); output.innerHTML=”<p>Original: “+a+”, “+b+”</p>”; b = [a, a = b][0]; output.innerHTML+=”<p>Swapped: “+a+”, “+b+”</p>”; <div id=”output”></div>
DuckDucking turned up this by Ken. Surprisingly, it’s even more concise and complete than Nikita’s answer. It retrieves column and row lengths implicitly within the guts of map(). function transpose(a) { return Object.keys(a[0]).map(function(c) { return a.map(function(r) { return r[c]; }); }); } console.log(transpose([ [1,2,3], [4,5,6], [7,8,9] ]));
This is not specifically about swap but an example showing that low level optimizations are maybe not worth the trouble. The compiler often figures it out anyway. Of course, this is my favorite example where the compiler is exceptionally lucky, but anyway we shouldn’t assume that compilers are stupid and that we can easily improve … Read more
I think this is the closest you can get to a simple swap, but it does not have a straightforward usage pattern: int swap(int a, int b) { // usage: y = swap(x, x=y); return a; } y = swap(x, x=y); It relies on the fact that x will pass into swap before y is … Read more
Here’s an interesting way to solve this using only jQuery (if the 2 elements are next to each other): $(“#element1”).before($(“#element2”)); or $(“#element1”).after($(“#element2”));
Assuming what you need is a simple byte swap, try something like Unsigned 16 bit conversion: swapped = (num>>8) | (num<<8); Unsigned 32-bit conversion: swapped = ((num>>24)&0xff) | // move byte 3 to byte 0 ((num<<8)&0xff0000) | // move byte 1 to byte 2 ((num>>8)&0xff00) | // move byte 2 to byte 1 ((num<<24)&0xff000000); // … Read more
17.6.5.15 [lib.types.movedfrom] Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state. When an object is in an unspecified state, you can perform any operation on the … Read more