Swap two elements in an array in Java

In this blog, you will see simple logic to swap the elements of the array, and also you can learn about Java.util.Collections Swap method.

Simple Swapping logic in Java

public class SwapElementsExample {

    public static void main(String[] args) {
        String[] arr = {"First", "Second", "Third", "Fourth"};

        System.out.println("Array before Swap" + "\n");
        for (String element : arr) {
            System.out.println(element);
        }

        //Simple Swapping logic
        String temp = arr[1];
        arr[1] = arr[2];
        arr[2] = temp;

        System.out.println("\n" + "Array after Swap" + "\n");
        for (String element : arr) {
            System.out.println(element);
        }
    }
}
Output
Array before Swap

First
Second
Third
Fourth

Array after Swap

First
Third
Second
Fourth

Collections.swap()

This method is used to swap the item to the specified positions within the list.

Syntax

public static void swap(List list, int a, int b);

list – the specified list where the elements are to be swapped

a – Index of the element to be swapped.

b – index of another element to be swapped

If the a or b is out of the range of the list, then the method throws the IndexOutOfBoundsException.


Example

import java.util.ArrayList;
import java.util.Collections;

public class SwapElementsExample {

    public static void main(String[] args) {
        ArrayList<String> arrList = new ArrayList<String>();
        //adding the elements in Array List
        arrList.add("1. Anupam Mittal");
        arrList.add("2. Aman Gupta");
        arrList.add("3. Namita Thapar");
        arrList.add("4. Ashneer Grover");
        arrList.add("5. Peyush Bansal");

        System.out.println("Current ArrayList " + arrList);

        System.out.println("\n" + "-- Elements in Arraylist before swap --");
        for (String element : arrList) {
            System.out.println(element);
        }

        //using the swap method 
        Collections.swap(arrList, 1, 4);
        System.out.println("\n" + "Using Collections.swap(arrList, 1, 4)");

        System.out.println("\n" + "Current ArrayList " + arrList);

        System.out.println("\n" + "-- Elements in Arraylist after swap --");
        for (String element : arrList) {
            System.out.println(element);
        }
    }
}
Output
Current ArrayList [1. Anupam Mittal, 2. Aman Gupta, 3. Namita Thapar, 4. Ashneer Grover, 5. Peyush Bansal]

-- Elements in Arraylist before swap --
1. Anupam Mittal
2. Aman Gupta
3. Namita Thapar
4. Ashneer Grover
5. Peyush Bansal

Using Collections.swap(arrList, 1, 4)

Current ArrayList [1. Anupam Mittal, 5. Peyush Bansal, 3. Namita Thapar, 4. Ashneer Grover, 2. Aman Gupta]

-- Elements in Arraylist after swap --
1. Anupam Mittal
5. Peyush Bansal
3. Namita Thapar
4. Ashneer Grover
2. Aman Gupta

That’s enough for a quick overview of Swapping elements of an array in Java.
Thank you

Leave a Reply

Your email address will not be published.