Java ArrayList Swap swap(ArrayList data, int i, int j)

Here you can find the source of swap(ArrayList data, int i, int j)

Description

Swap two values in an object array.

License

Open Source License

Parameter

Parameter Description
data Data
i First position
j Second position
T Object type

Declaration

public static <T> void swap(ArrayList<T> data, int i, int j) 

Method Source Code


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

import java.util.ArrayList;

public class Main {
    /**// ww w.j  a va  2  s.  c o m
     * Swap two values in an object array.
     * 
     * @param data
     *            Data
     * @param i
     *            First position
     * @param j
     *            Second position
     * @param <T>
     *            Object type
     */
    public static <T> void swap(ArrayList<T> data, int i, int j) {
        T tmp = data.get(i);
        data.set(i, data.get(j));
        data.set(j, tmp);
    }

    /**
     * Swap two values in an object array.
     * 
     * @param data
     *            Data
     * @param i
     *            First position
     * @param j
     *            Second position
     * @param <T>
     *            Object type
     */
    public static <T> void swap(T[] data, int i, int j) {
        T tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }

    /**
     * Swap two values in an integer array.
     * 
     * @param data
     *            Data
     * @param i
     *            First position
     * @param j
     *            Second position
     */
    public static void swap(int[] data, int i, int j) {
        int tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }

    /**
     * Swap two values in a byte array.
     * 
     * @param data
     *            Data
     * @param i
     *            First position
     * @param j
     *            Second position
     */
    public static void swap(byte[] data, int i, int j) {
        byte tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }

    /**
     * Swap two values in a double array.
     * 
     * @param data
     *            Data
     * @param i
     *            First position
     * @param j
     *            Second position
     */
    public static void swap(double[] data, int i, int j) {
        double tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }

    /**
     * Swap two values in a float array.
     * 
     * @param data
     *            Data
     * @param i
     *            First position
     * @param j
     *            Second position
     */
    public static void swap(float[] data, int i, int j) {
        float tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;
    }
}

Related

  1. swap(ArrayList numbers, int i, int j)
  2. swap(ArrayList dataStrs, int i, int j)
  3. swapRemove(ArrayList list, int index)