java.lang.Object
org.cicirello.util.SimpleSwapper
A utility class for performing element swaps. The methods of this class assume that indexes are
different without verifying, although this will function correctly regardless. If your
application is likely to frequently pass identical indexes, then use
ValidatedSwapper
instead. However, if your application is such that the indexes are significantly more likely
different, then just use this SimpleSwapper class to avoid unnecessary comparisons. As an example
of an application where SimpleSwapper is likely preferred, consider the case of randomizing the
ordering of the elements of an array. The expected number of fixed points in a random permutation
is 1, so the majority of randomly chosen swaps in such an application that lead to identical
indexes will be very low.-
Method Summary
Modifier and TypeMethodDescriptionstatic void
swap
(byte[] array, int i, int j) Swaps the elements at two indexes of an array.static void
swap
(char[] array, int i, int j) Swaps the elements at two indexes of an array.static void
swap
(double[] array, int i, int j) Swaps the elements at two indexes of an array.static void
swap
(float[] array, int i, int j) Swaps the elements at two indexes of an array.static void
swap
(int[] array, int i, int j) Swaps the elements at two indexes of an array.static void
swap
(long[] array, int i, int j) Swaps the elements at two indexes of an array.static void
swap
(short[] array, int i, int j) Swaps the elements at two indexes of an array.static void
Swaps the elements at two indexes of an array.static <T> void
Swaps the elements at two indexes of an list.
-
Method Details
-
swap
public static void swap(byte[] array, int i, int j) Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
public static void swap(char[] array, int i, int j) Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
public static void swap(double[] array, int i, int j) Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
public static void swap(float[] array, int i, int j) Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
public static void swap(int[] array, int i, int j) Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
public static void swap(long[] array, int i, int j) Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
public static void swap(short[] array, int i, int j) Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
Swaps the elements at two indexes of an array.- Parameters:
array
- the array within which to swap elementsi
- one indexj
- another index
-
swap
Swaps the elements at two indexes of an list.- Type Parameters:
T
- the type of elements in the List- Parameters:
list
- the list within which to swap elementsi
- one indexj
- another index
-