To write a swap routine that works in Java, we need to pass a reference to an object so that when integers inside the object are changed, these changes are visible on the outside. One way to do this is to use two arrays with one element.
    public void swap(int[] a, int[] b)
        int temp = a[0];
        a[0] = b[0];
        b[0] = temp;

Unfortunately, code that uses this swap method must know to package their variables into one element arrays before calling swap.

For example:

    int[] x = new int[1];
    int[] y = new int[1];
    x[0] = 20;
    y[0] = 15;
    swap(x, y);

Last modified: Tue Aug 19 22:54:43 EDT