Java Array Swap swap(T[] array, int a, int b)

Here you can find the source of swap(T[] array, int a, int b)

Description

Swaps the elements at the given positions in the array.

License

Open Source License

Parameter

Parameter Description
array the array containing the elements to swap
a the index for the first element
b the index for the second element

Declaration

public static <T> void swap(T[] array, int a, int b) 

Method Source Code

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

public class Main {
    /**/*from  www .j a v  a  2 s  .  c o m*/
     *  Swaps the elements at the given positions in the array.
     *
     *  @param array the array containing the elements to swap
     *  @param a     the index for the first element
     *  @param b     the index for the second element
     */
    public static void swap(int[] array, int a, int b) {
        array[a] ^= array[b];
        array[b] = array[a] ^ array[b];
        array[a] ^= array[b];
    }

    /**
     *  Swaps the elements at the given positions in the array.
     *
     *  @param array the array containing the elements to swap
     *  @param a     the index for the first element
     *  @param b     the index for the second element
     */
    public static <T> void swap(T[] array, int a, int b) {
        T temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }
}

Related

  1. swap(T[] a, int idx1, int idx2)
  2. swap(T[] arr, int a, int b)
  3. swap(T[] arr, int i, int j)
  4. swap(T[] arr, int index1, int index2)
  5. swap(T[] arr, int pos1, int pos2)
  6. swap(T[] array, int first_idx, int last_idx)
  7. swap(T[] array, int i, int len)
  8. swap(T[] array, int indexOne, int indexTwo)
  9. swap(T[] data, int a, int b)