How to write a generic swap function in Java

1 Answer

0 votes
import java.util.Arrays;

/**
 * generic_swap
 * ------------
 * A fully generic Java swap function implemented using generics.
 *
 * Parameters:
 *   a, b  - wrapper objects whose internal values will be swapped
 *
 * Java does not allow swapping primitives directly because they are
 * passed by value. To emulate C++ reference semantics, we wrap primitive
 * values in small mutable classes (IntBox, DoubleBox).
 *
 * This function works for:
 *   - boxed primitive wrappers (IntBox, DoubleBox)
 *   - Strings (reference swap)
 *   - user‑defined classes
 */
class Swap {
    public static <T> void genericSwap(Box<T> a, Box<T> b) {
        T temp = a.value;
        a.value = b.value;
        b.value = temp;
    }
}

/**
 * Simple generic wrapper class to simulate pass‑by‑reference.
 */
class Box<T> {
    public T value;
    public Box(T value) { this.value = value; }
}

/**
 * Mutable wrappers for primitive types
 */
class IntBox extends Box<Integer> {
    public IntBox(int v) { super(v); }
}

class DoubleBox extends Box<Double> {
    public DoubleBox(double v) { super(v); }
}

/**
 * Java equivalent of the C++ struct Point
 */
class Point {
    public int x, y;
    public Point(int x, int y) { this.x = x; this.y = y; }

    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
}

/**
 * Print arrays of integers
 */
class Utils {
    public static void printIntArray(int[] arr) {
        for (int v : arr)
            System.out.print(v + " ");
        System.out.println();
    }
}

public class Main {
    public static void main(String[] args) {

        System.out.println("=== TEST 1: Swap integers ===");
        IntBox x = new IntBox(10);
        IntBox y = new IntBox(20);
        System.out.println("Before: x=" + x.value + ", y=" + y.value);
        Swap.genericSwap(x, y);
        System.out.println("After:  x=" + x.value + ", y=" + y.value + "\n");

        System.out.println("=== TEST 2: Swap doubles ===");
        DoubleBox a = new DoubleBox(3.14);
        DoubleBox b = new DoubleBox(2.71);
        System.out.println("Before: a=" + a.value + ", b=" + b.value);
        Swap.genericSwap(a, b);
        System.out.println("After:  a=" + a.value + ", b=" + b.value + "\n");

        System.out.println("=== TEST 3: Swap structs ===");
        Point p1 = new Point(1, 2);
        Point p2 = new Point(9, 8);
        System.out.println("Before: p1=" + p1 + ", p2=" + p2);
        Box<Point> bp1 = new Box<>(p1);
        Box<Point> bp2 = new Box<>(p2);
        Swap.genericSwap(bp1, bp2);
        System.out.println("After:  p1=" + bp1.value + ", p2=" + bp2.value + "\n");

        System.out.println("=== TEST 4: Swap array elements ===");
        int[] arr = {1, 2, 3, 4, 5};
        System.out.print("Before: ");
        Utils.printIntArray(arr);
        int temp = arr[1];
        arr[1] = arr[3];
        arr[3] = temp;
        System.out.print("After:  ");
        Utils.printIntArray(arr);
        System.out.println();

        System.out.println("=== TEST 5: Swap strings ===");
        Box<String> str1 = new Box<>("Hello");
        Box<String> str2 = new Box<>("World");
        System.out.println("Before: str1=\"" + str1.value + "\", str2=\"" + str2.value + "\"");
        Swap.genericSwap(str1, str2);
        System.out.println("After:  str1=\"" + str1.value + "\", str2=\"" + str2.value + "\"\n");
    }
}


/*
run:

=== TEST 1: Swap integers ===
Before: x=10, y=20
After:  x=20, y=10

=== TEST 2: Swap doubles ===
Before: a=3.14, b=2.71
After:  a=2.71, b=3.14

=== TEST 3: Swap structs ===
Before: p1=(1,2), p2=(9,8)
After:  p1=(9,8), p2=(1,2)

=== TEST 4: Swap array elements ===
Before: 1 2 3 4 5 
After:  1 4 3 2 5 

=== TEST 5: Swap strings ===
Before: str1="Hello", str2="World"
After:  str1="World", str2="Hello"

*/

 



answered 7 hours ago by avibootz
...