Here you can find the source of swap(int[] array)
Parameter | Description |
---|---|
array | the array with two elements to swap |
public static void swap(int[] array)
//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; } } }