Here you can find the source of swap(T[] arr, int a, int b)
a
and b
in the array arr
.
Parameter | Description |
---|---|
T | Type of array elements |
arr | Array that contains the elements to be swapped |
a | First position |
b | Second position |
public static <T> void swap(T[] arr, int a, int b)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w w w . ja v a 2 s. c om * Swaps the elements at position <code>a</code> and <code>b</code> in the * array <code>arr</code>. * * @param <T> * Type of array elements * @param arr * Array that contains the elements to be swapped * @param a * First position * @param b * Second position */ public static <T> void swap(T[] arr, int a, int b) { if (a < 0 || a > arr.length || b < 0 || b > arr.length) throw new IllegalArgumentException("swap position out of bounds."); if (a != b) { T t = arr[a]; arr[a] = arr[b]; arr[b] = t; } } }