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

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

Description

Swaps two entries in a T array.

License

Open Source License

Parameter

Parameter Description
T The type of the array.
arr array
i position of first element
j position of second element

Declaration

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

Method Source Code

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

public class Main {
    /**/*from  w w w  . j a v a2  s .  co  m*/
     * Swaps two entries in a <code>T</code> array. For lists use
     * {@link java.util.Collections#swap(List, int, int)}.
     *
     * @param <T> The type of the array.
     * @param arr array
     * @param i position of first element
     * @param j position of second element
     */
    public static <T> void swap(final T[] arr, final int i, final int j) {
        final T temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    /**
     * Swaps two entries in a <code>double</code> array.
     *
     * @param arr array
     * @param i position of first element
     * @param j position of second element
     * @see #swap(Object[], int, int)
     */
    public static void swap(final double[] arr, final int i, final int j) {
        final double temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    /**
     * Swaps two entries in an <code>int</code> array.
     *
     * @param arr array
     * @param i position of first element
     * @param j position of second element
     * @see #swap(Object[], int, int)
     */
    public static void swap(final int[] arr, final int i, final int j) {
        final int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

Related

  1. swap(final Object[] array, int pos)
  2. swap(final Object[] data, final int i, final int j)
  3. swap(final Object[] values, final int firstPosition, final int secondPosition)
  4. swap(final String a[], final int i, final int j)
  5. swap(final T[] arr, final int i, final int j)
  6. swap(int arr[], int i, int j)
  7. swap(int array[], int first, int last)
  8. swap(int arrs[], int i, int j)
  9. swap(int i, int j, Integer[] unsorted)