Swapping elements in a Java array

In Java, arrays are a fixed-size data structure that stores elements of the same type in contiguous memory locations. At times, you may need to swap elements within an array. Swapping involves interchanging the values of two elements in the array.

In this article, we will explore two approaches to swapping elements in a Java array. Let’s get started!

1. Using a Temporary Variable

One way to swap elements in a Java array is by using a temporary variable. Here’s an example:

public class ArraySwapper {
    public static void main(String[] args) {
        int[] numbers = { 5, 10, 15, 20, 25 };

        int index1 = 1;
        int index2 = 3;

        // Swapping elements using a temporary variable
        int temp = numbers[index1];
        numbers[index1] = numbers[index2];
        numbers[index2] = temp;

        // Print the updated array
        for (int number : numbers) {
            System.out.println(number);
        }
    }
}

In the above example, we have an array called numbers containing five elements. We want to swap the element at index1 (which is 10) with the element at index2 (which is 20).

To swap the elements, we use a temporary variable called temp to store the value of numbers[index1]. Then, we assign the value of numbers[index2] to numbers[index1] and finally, assign temp to numbers[index2]. This way, the elements are swapped successfully.

The output of the above code will be:

5
20
15
10
25

2. Swapping without a Temporary Variable

Another approach to swap elements in a Java array is by performing bitwise XOR operations. This method does not require using a temporary variable. Here’s an example:

public class ArraySwapper {
    public static void main(String[] args) {
        int[] numbers = { 5, 10, 15, 20, 25 };

        int index1 = 1;
        int index2 = 3;

        // Swapping elements without using a temporary variable
        numbers[index1] = numbers[index1] ^ numbers[index2];
        numbers[index2] = numbers[index1] ^ numbers[index2];
        numbers[index1] = numbers[index1] ^ numbers[index2];

        // Print the updated array
        for (int number : numbers) {
            System.out.println(number);
        }
    }
}

In this example, we use bitwise XOR operations (^) to swap the elements without using a temporary variable. XOR is a binary operation that produces 1 if the corresponding bits are different, and 0 if they are the same. By performing XOR operations on the elements, we can achieve the swapping effect.

The output of the above code will be the same as the previous example:

5
20
15
10
25

Conclusion

Swapping elements in a Java array can be achieved using either a temporary variable or bitwise XOR operations. Both approaches accomplish the same goal - interchanging the values of two elements. Choose the method that suits your needs and programming style.

Keep in mind that when swapping elements, it is important to check the validity of the array indices to prevent any potential errors.

Happy coding! #Java #ArraySwapping