In Java, the procedure of swapping two variables refers to the changing positions of the values of variables. We can swap variables from two separate positions in an array. There are numerous methods for swapping elements in an array; however, this operation will generate an error if the size of both arrays is different. Therefore, the arrays that need to be swapped should have the same size.
This post will tell you how to swap arrays in Java.
How to Swap Arrays in Java?
For swapping arrays, Java supports three approaches mentioned below:
- Arithmetic operators
- Bitwise operators
- Using the temp variable
Let’s try to understand these approaches for swapping arrays in Java.
Method 1: Swap Arrays in Java Using Arithmetic Operators
For swapping arrays, you can use the Java arithmetic operators “+” and “-”. In this approach, the first element of the first array, “x”, and the first element of the second array “y”, are fetched and subtracted using the “-” operator and will be stored in the first index of the first array. Now, use the updated value of “x” with the first element of “y” and add them using the “+” operator and store it on the first index of the array “y”.
Then, take the absolute value of the first element of the first array and the first element of the second array and subtract them by using the “-” operator and store it in the first index of the first element. This process continues until the array’s length, and all the elements will be swapped.
Syntax
Follow the given syntax for swapping array by using Arithmetic operators:
y[i] = x[i] + y[i];
x[i] = absoluteValue(x[i] - y[i]);
The “x” and “y” are the arrays, while “i” represents the indexes of the array during traversal in the for loop.
Example
In this example, first, we will create a user-defined method named “absoluteValue()” and pass an integer type variable “x” as a parameter. As the return statement, the “Math.abs()” method is called, which accepts the passed variable as an argument. The absolute value of the passed argument is returned by this method. If the argument is not negative, it outputs the same value; else, the negation of the argument is returned:
return Math.abs(x);
}
In the main() method, we will create two arrays “x” and “y” of size “3” and assign them the following values:
int y[] = {2,4,6};
Then, we will use the arithmetic operators to swap the arrays elements using “for” loop:
x[i] = x[i] - y[i];
y[i] = x[i] + y[i];
x[i] = absoluteValue(x[i] - y[i]);
}
Finally, we will print the sorted elements of arrays “x” and “y” by utilizing the toString() method of Arrays class:
System.out.println("Array y[] after swapping : "+ Arrays.toString(y));
The output shows that the values of arrays have been successfully swapped:
Let’s see the next method for swapping arrays in Java which is Bitwise operators.
Method 2: Swap Arrays in Java Using Bitwise Operators
We can also swap arrays using bitwise operators (^) in which the XOR operation is performed on input bit by bit. It outputs “1” if the associated bits are different; else, it returns “0”.
Syntax
Follow the given syntax to use bitwise operators for the specified purpose:
Example
Here, we will use the same arrays “x” and “y” and perform bitwise operation to swap arrays:
x[i] = x[i] ^ y[i];
y[i] = x[i] ^ y[i];
x[i] = x[i] ^ y[i];
}
Print the sorted elements of arrays using the “toString()” method of Arrays class:
System.out.println("Array y[] after swapping : "+ Arrays.toString(y));
Output
If you feel that the above methods are difficult, then you can follow the below-given method.
Method 3: Swap Arrays in Java Using “temp” Variable
This method will use a temporary variable “temp” that stores the elements temporarily for swapping arrays. In this method, no special operation will be performed; you only have to store the first element in the temporary variable, add the other array’s element in place of it, and then swap the temporary element value in the second array’s element.
Syntax
For swapping arrays using the temporary variable, follow the below-given syntax:
x[i] = y[i];
y[i]=temp;
Here, “temp” is a temporary variable, “x” and “y” are the arrays and “i” represent their current indexes.
Example
We will use the same arrays “x” and “y” and swap the arrays using the “temp” variable. To do so, first check the size of the arrays, if they are equal then we will swap them; otherwise, print the error statement on the console:
int temp;
for (int i=0; i<x.length; i++){
temp = x[i];
x[i] = y[i];
y[i]=temp;
}
} else{
System.out.println("Different size arrays cannot be swapped");
}
Print the sorted elements of arrays on the console:
System.out.println("Array y[] after swapping : "+ Arrays.toString(y));
As you can see, we have successfully swapped the values of the specified arrays:
We have provided the easiest methods for swapping arrays in Java.
Conclusion
For swapping arrays in Java, you can use arithmetic operators, bitwise operators, or the temp variable. The first two techniques need no extra variable or array for swapping elements of arrays, while the third method uses the temp variable and requires you to create an extra variable to store the elements temporarily. In this post, we explained the methods for swapping arrays in Java with detailed examples.