Java Array Swap swap(short x[], T[] a2, int a, int b)

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

Description

Swaps x[a] with x[b].

License

Apache License

Declaration

private static <T> void swap(short x[], T[] a2, int a, int b) 

Method Source Code

//package com.java2s;
/**/*w w  w .j  av a 2s .co  m*/
 * 
 *    Copyright 2017 Florian Erhard
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

public class Main {
    public static <T> void swap(T[] a, int i, int j) {
        T temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

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

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

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(long x[], T[] a2, int a, int b) {
        long t = x[a];
        x[a] = x[b];
        x[b] = t;
        T t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(int x[], T[] a2, int a, int b) {
        int t = x[a];
        x[a] = x[b];
        x[b] = t;
        T t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(int x[], double[] a2, int a, int b) {
        int t = x[a];
        x[a] = x[b];
        x[b] = t;
        double t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static void swap(int x[], int[] a2, int a, int b) {
        int t = x[a];
        x[a] = x[b];
        x[b] = t;
        int t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(short x[], T[] a2, int a, int b) {
        short t = x[a];
        x[a] = x[b];
        x[b] = t;
        T t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(char x[], T[] a2, int a, int b) {
        char t = x[a];
        x[a] = x[b];
        x[b] = t;
        T t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static void swap(char x[], char[] a2, int a, int b) {
        char t = x[a];
        x[a] = x[b];
        x[b] = t;
        char t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(byte x[], T[] a2, int a, int b) {
        byte t = x[a];
        x[a] = x[b];
        x[b] = t;
        T t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(double x[], T[] a2, int a, int b) {
        double t = x[a];
        x[a] = x[b];
        x[b] = t;
        T t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static void swap(double x[], int[] a2, int a, int b) {
        double t = x[a];
        x[a] = x[b];
        x[b] = t;
        int t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static void swap(double x[], double[] a2, int a, int b) {
        double t = x[a];
        x[a] = x[b];
        x[b] = t;
        double t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T> void swap(float x[], T[] a2, int a, int b) {
        float t = x[a];
        x[a] = x[b];
        x[b] = t;
        T t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T, S> void swap(T x[], S[] a2, int a, int b) {
        T t = x[a];
        x[a] = x[b];
        x[b] = t;
        S t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static <T, S> void swap(T x[], int[] a2, int a, int b) {
        T t = x[a];
        x[a] = x[b];
        x[b] = t;
        int t2 = a2[a];
        a2[a] = a2[b];
        a2[b] = t2;
    }
}

Related

  1. swap(Object[] array)
  2. swap(Object[] array, int a, int b)
  3. swap(Object[] x, int a, int b)
  4. swap(short x[], int a, int b)
  5. swap(short x[], int[] y, int a, int b)
  6. swap(short[] array, int indexA, int indexB)
  7. swap(String a[], int i, int j)
  8. swap(T[] a, int i1, int i2)
  9. swap(T[] a, int idx1, int idx2)