Java Array Swap swap(T[] arr, int pos1, int pos2)

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

Description

Exchanges two elements in the array.

License

Open Source License

Parameter

Parameter Description
arr the array containing the elements to swap
pos1 the position of the first element
pos2 the position of the second element

Declaration

public static <T extends Comparable<T>> void swap(T[] arr, int pos1, int pos2) 

Method Source Code

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

public class Main {
    /**/*w w  w.  j  a  v a  2  s .  c  o m*/
     * Exchanges two elements in the array. 
     * @param arr the array containing the elements to swap
     * @param pos1 the position of the first element
     * @param pos2 the position of the second element
     */
    public static <T extends Comparable<T>> void swap(T[] arr, int pos1, int pos2) {
        final T temp = arr[pos1];
        arr[pos1] = arr[pos2];
        arr[pos2] = temp;
    }
}

Related

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