Class SimpleSwapper

java.lang.Object
org.cicirello.util.SimpleSwapper

public final class SimpleSwapper extends Object
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 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