java.lang.Object
org.cicirello.util.ValidatedSwapper
A utility class for performing element swaps. The methods of this class verify indexes are
different to avoid unnecessary element assignment. If your application is one where the indexes
are very likely different, then use
SimpleSwapper
instead, which does not check the
indexes. 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, so in that case most of the checks
for identical indexes would be unnecessary increasing overall cost.-
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
-