Class ValidatedSwapper

java.lang.Object
org.cicirello.util.ValidatedSwapper

public final class ValidatedSwapper extends Object
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 Type
    Method
    Description
    static 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
    swap(Object[] array, int i, int j)
    Swaps the elements at two indexes of an array.
    static <T> void
    swap(List<T> list, int i, int j)
    Swaps the elements at two indexes of an list.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 elements
      i - one index
      j - 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 elements
      i - one index
      j - 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 elements
      i - one index
      j - 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 elements
      i - one index
      j - 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 elements
      i - one index
      j - 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 elements
      i - one index
      j - 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 elements
      i - one index
      j - another index
    • swap

      public static void swap(Object[] array, int i, int j)
      Swaps the elements at two indexes of an array.
      Parameters:
      array - the array within which to swap elements
      i - one index
      j - another index
    • swap

      public static <T> void swap(List<T> list, int i, int j)
      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 elements
      i - one index
      j - another index