I am new to java. How to write the java equivalent of the following C code.
void Swap(int *p, int *q) { int temp; temp = *p; *p = *q; *q = temp; }
The swap() method of java. util. Collections class is used to swap the elements at the specified positions in the specified list. If the specified positions are equal, invoking this method leaves the list unchanged.
What is the syntax of swap()? Clarification: The correct syntax of swap function is arr1. swap(arr2) i.e. one array calling swap() function with second array as parameter to swap function.
Two variables can be swapped in one line in Java. This is done by using the given statement. x = x ^ y ^ (y = x); where x and y are the 2 variables.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With