Java Array Swap swap(T[] arr, int i, int j)

Here you can find the source of swap(T[] arr, int i, int j)

Description

Swap values in array

License

Open Source License

Parameter

Parameter Description
arr array
i index i
j index j

Declaration

private static <T> void swap(T[] arr, int i, int j) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   ww  w. j a v  a 2 s  .co  m
     * Swap values in array
     *
     * @param arr array
     * @param i   index i
     * @param j   index j
     */
    private static <T> void swap(T[] arr, int i, int j) {
        T t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }

    /**
     * swap ints at given positions in array
     *
     * @param arr array
     * @param i   position
     * @param j   position
     */
    public static void swap(int[] arr, int i, int j) {
        arr[i] ^= arr[j];
        arr[j] ^= arr[i];
        arr[i] ^= arr[j];
    }
}

Related

  1. swap(short[] array, int indexA, int indexB)
  2. swap(String a[], int i, int j)
  3. swap(T[] a, int i1, int i2)
  4. swap(T[] a, int idx1, int idx2)
  5. swap(T[] arr, int a, int b)
  6. swap(T[] arr, int index1, int index2)
  7. swap(T[] arr, int pos1, int pos2)
  8. swap(T[] array, int a, int b)
  9. swap(T[] array, int first_idx, int last_idx)