Java Program to Swap Two Arrays without temp

Write a Java Program to Swap Two Arrays without a temp variable. Instead of using the third variable, we are going to use Arithmetic and Bitwise Operators.

Java Program to Swap Two Arrays without Temp variable

In this Java program, we are going to use Arithmetic Operators to swap two arrays.

// Java Program to Swap Two Array without temp
import java.util.Scanner;
public class SwapArrays1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		int [] b = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of First Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}
		
		System.out.print(" Please Enter " + Size + " elements of Second Array  : ");
		for (i = 0; i < Size; i++)
		{
			b[i] = sc.nextInt();
		}
		
		for(i = 0; i < Size; i++)
		{
		    a[i] = a[i] + b[i];
		    b[i] = a[i] - b[i]; 
		    a[i] = a[i] - b[i];
		}
		
		System.out.print("\n First  Array Elements (a[]) After Swapping :  ");
		printAttay(a, Size);
		System.out.print("\n Second Array Elements (b[]) After Swapping :  ");
		printAttay(b, Size);
	}
	public static void printAttay(int[] Array, int Size)
	{
		for (int Number: Array) 
		{
			System.out.print(Number + " ");
		}
	}
}
Java Program to Swap Two Arrays without temp variable 1

First, we used For Loop to iterate each element.

for(i = 0; i < Size; i++)
{
	a[i] = a[i] + b[i];
	b[i] = a[i] - b[i]; 
	a[i] = a[i] - b[i];
}

User inserted values are
a[3] = {10, 20, 30} and b[3] = {45, 55, 65}

First Iteration: for (i = 0; 0 < 3; 0++)
The value of i will be 0, and the condition (i < 3) is True. So, Java will start executing the Arithmetic Operator statements inside the loop until the condition fails.

a[i] = a[i] + b[i]
a[0] = a[0] + b[0]
a[0] = 10 + 45 = 55

b[i] = a[i] – b[i]
b[0] = a[0] – b[0]
b[0] = 50 – 45 = 10

a[i] = a[i] – b[i]
a[0] = a[0] – b[0]
a[0] = 55 – 10 = 45

Second Iteration: for (i = 1; 1 < 3; 1++)
The condition (i < 3) is True.

a[1] = a[1] + b[1]
a[1] = 20 + 55 = 75

b[1] = a[1] – b[1]
b[1] = 75 – 55 = 20

a[1] = a[1] – b[1]
a[0] = 75 – 20 = 55

Third Iteration: for (i = 2; 2 < 3; 2++)
The condition (i < 3) is True.

a[2] = a[2] + b[2] => 30 + 65 = 95

b[1] = a[1] – b[1] => 95 – 65 = 30

a[1] = a[1] – b[1] => 95 – 30 = 65

NOTE: In Java, although we can swap two arrays using multiplication and division approach but they may produce strange values if we are working with larger integer values

Java Program to Swap Two Arrays without Temp Example 2

In this program, instead of using a temp or third variable, we are going to use Bitwise OR Operator. Please refer Bitwise Operator in Java to understand how the bitwise operator works.

// Java Program to Swap Two Array without temp variable
import java.util.Scanner;

public class SwapArrays2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		int [] b = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of First Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}
		
		System.out.print(" Please Enter " + Size + " elements of Second Array  : ");
		for (i = 0; i < Size; i++)
		{
			b[i] = sc.nextInt();
		}
		
		for(i = 0; i < Size; i++)
		{
			a[i] = a[i] ^ b[i];
		    b[i] = a[i] ^ b[i]; 
		    a[i] = a[i] ^ b[i];
		}
		
		System.out.print("\n First  Array Elements (a[]) After Swapping :  ");
		printAttay(a, Size);
		System.out.print("\n Second Array Elements (b[]) After Swapping :  ");
		printAttay(b, Size);
	}
	public static void printAttay(int[] Array, int Size)
	{
		for (int Number: Array) 
		{
			System.out.print(Number + " ");
		}
	}
}

Java swap arrays using Bitwise operators output

 Please Enter Number of elements in an array : 5
 Please Enter 5 elements of First Array  : 5 15 25 35 45
 Please Enter 5 elements of Second Array  : 110 120 130 140 150

 First  Array Elements (a[]) After Swapping :  110 120 130 140 150 
 Second Array Elements (b[]) After Swapping :  5 15 25 35 45 

Java Program to Swap Two Arrays without Temp Example 2

This Java program is the same as the first example. Here, we separated the logic to swap two arrays using Method.

// Java Program to Swap Two Array without temp variable
import java.util.Scanner;

public class SwapArrays3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i;
		sc = new Scanner(System.in);
	 
		System.out.print(" Please Enter Number of elements in an array : ");
		Size = sc.nextInt();	
		
		int [] a = new int[Size];
		int [] b = new int[Size];
		
		System.out.print(" Please Enter " + Size + " elements of First Array  : ");
		for (i = 0; i < Size; i++)
		{
			a[i] = sc.nextInt();
		}
		
		System.out.print(" Please Enter " + Size + " elements of Second Array  : ");
		for (i = 0; i < Size; i++)
		{
			b[i] = sc.nextInt();
		}

		swapArrays(a, b, Size);
	}
	public static void swapArrays(int[] a, int[] b, int Size)
	{
		int i;
		for(i = 0; i < Size; i++)
		{
		    a[i] = a[i] + b[i];
		    b[i] = a[i] - b[i]; 
		    a[i] = a[i] - b[i];
		}
		System.out.print("\n First  Array Elements (a[]) After Swapping :  ");
		printAttay(a, Size);
		System.out.print("\n Second Array Elements (b[]) After Swapping :  ");
		printAttay(b, Size);
	}
	public static void printAttay(int[] Array, int Size)
	{
		for (int Number: Array) 
		{
			System.out.print(Number + " ");
		}
	}
}

Java swap arrays using arithmetic operators output

Please Enter Number of elements in an array : 10
 Please Enter 10 elements of First Array  : 10 20 30 40 50 60 70 80 90  100
 Please Enter 10 elements of Second Array  : 5 15 25 35 45 55 65 75 85 95

 First  Array Elements (a[]) After Swapping :  5 15 25 35 45 55 65 75 85 95 
 Second Array Elements (b[]) After Swapping :  10 20 30 40 50 60 70 80 90 100