Class CombinationIterator


  • public class CombinationIterator
    extends java.lang.Object
    Iterate through all combinations (n choose k) and provide permutations representing the combinations. Designed to work in permutation tests, where instead of randomization all combinations are tested.
    • Constructor Summary

      Constructors 
      Constructor Description
      CombinationIterator​(int totalCount, int subCount)
      Instantiate the iterator.
    • Constructor Detail

      • CombinationIterator

        public CombinationIterator​(int totalCount,
                                   int subCount)
        Instantiate the iterator.
        Parameters:
        totalCount - the total number of elements
        subCount - the number of elements to choose
    • Method Detail

      • getPermutation

        public int[] getPermutation()
        Returns:
        a permutation representing the combination
      • next

        public boolean next()
        Advance to the next combination. (First call stays in the initial combination.)
        Returns:
        true if there is a next combination
      • getNumberOfCombinations

        public static double getNumberOfCombinations​(int n,
                                                     int k)
        Computes the number of combinations. Returns a double because rounding to int will cause undesired results when the number is larger than max int.
        Parameters:
        n - total count
        k - sub count
        Returns:
        the number of combinations (n choose k)