Java Array Swap swap(T[] arr, int index1, int index2)

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

Description

Swaps two objects in an array.

License

Open Source License

Parameter

Parameter Description
arr the array to swap in
index1 an index of an element to swap
index2 an index of an element to swap

Declaration

public static <T> T[] swap(T[] arr, int index1, int index2) 

Method Source Code

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

public class Main {
    /**//from  ww w  .  j  av a  2  s.co m
     * Swaps two objects in an array.
     * @param arr the array to swap in
     * @param index1 an index of an element to swap
     * @param index2 an index of an element to swap
     */
    public static <T> T[] swap(T[] arr, int index1, int index2) {
        arr = arr.clone();
        T temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
        return arr;
    }
}

Related

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