Java Array Swap swap(int[] array)

Here you can find the source of swap(int[] array)

Description

Swaps the two integers in the array.

License

Open Source License

Parameter

Parameter Description
array the array with two elements to swap

Declaration

public static void swap(int[] array) 

Method Source Code

//package com.java2s;
/*/*  ww w .  j av a  2s.  c  o  m*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Swaps the two integers in the array.
     * 
     * @param array   the array with two elements to swap
     */
    public static void swap(int[] array) {
        int tmp;

        if (array.length == 2) {
            tmp = array[0];
            array[0] = array[1];
            array[1] = tmp;
        }
    }

    /**
     * Swaps the two longs in the array.
     * 
     * @param array   the array with two elements to swap
     */
    public static void swap(long[] array) {
        long tmp;

        if (array.length == 2) {
            tmp = array[0];
            array[0] = array[1];
            array[1] = tmp;
        }
    }

    /**
     * Swaps the two floats in the array.
     * 
     * @param array   the array with two elements to swap
     */
    public static void swap(float[] array) {
        float tmp;

        if (array.length == 2) {
            tmp = array[0];
            array[0] = array[1];
            array[1] = tmp;
        }
    }

    /**
     * Swaps the two doubles in the array.
     * 
     * @param array   the array with two elements to swap
     */
    public static void swap(double[] array) {
        double tmp;

        if (array.length == 2) {
            tmp = array[0];
            array[0] = array[1];
            array[1] = tmp;
        }
    }

    /**
     * Swaps the two objects in the array.
     * 
     * @param array   the array with two elements to swap
     */
    public static void swap(Object[] array) {
        Object tmp;

        if (array.length == 2) {
            tmp = array[0];
            array[0] = array[1];
            array[1] = tmp;
        }
    }
}

Related

  1. swap(int[] a, int i, int j)
  2. swap(int[] arr, int a, int b)
  3. swap(int[] arr, int a, int b)
  4. swap(int[] arr, int a, int b, int c, int d, int key)
  5. swap(int[] arr, int i, int j)
  6. swap(int[] array, int i, int j)
  7. swap(int[] array, int idx1, int idx2)
  8. swap(int[] array, int index0, int index1)
  9. swap(int[] array, int x, int y)