Java Array Swap swap(Object[] arr, int a, int b)

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

Description

Swaps arr[a] with arr[b] in the specified array.

License

Open Source License

Parameter

Parameter Description
arr the array we need to manipulate
a a parameter
b a parameter

Declaration

public static void swap(Object[] arr, int a, int b) 

Method Source Code

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

public class Main {

    public static void swap(Object[] arr, int a, int b) {
        Object tempObj = arr[a];//  w  w  w  .java  2s .  com
        arr[a] = arr[b];
        arr[b] = tempObj;
    }

    public static void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void swap(double[] arr, int a, int b) {
        double temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void swap(float[] arr, int a, int b) {
        float temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void swap(long[] arr, int a, int b) {
        long temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void swap(short[] arr, int a, int b) {
        short temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void swap(char[] arr, int a, int b) {
        char temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

    public static void swap(byte[] arr, int a, int b) {
        byte temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}

Related

  1. swap(int[] integers, int positionX, int positionY)
  2. swap(int[] list, int i, int j)
  3. swap(long[] a, int i, int change)
  4. swap(long[] keys, double[] values, int i, int j)
  5. swap(Object aobj[], int i, int j, int k)
  6. swap(Object[] arr, int i, int j)
  7. swap(Object[] arr, int i, int j)
  8. swap(Object[] array)
  9. swap(Object[] array, int a, int b)