T
- The type for the combinations.@Status(stage=PRODUCTION, unitTests=COMPLETE) @Review(by="Kees Schotanus", at="2009-09-28") @ThreadSafety(level=NOT_THREAD_SAFE) public final class Combinations<T> extends Object
a, b, c, d, e 0 1 2As you can see a, b , c is the first combination. To get the second combination increase the last pointer. This looks like:
a, b, c, d, e 0 1 2Increase the last pointer again, this results in:
a, b, c, d, e 0 1 2Now you can't increase the last pointer since it would be out of bounds. We have to increase the second pointer and let the third pointer point just after the second. This looks like:
a, b, c, d, e 0 1 2You just have to repeat this process to get all combinations! Check out the source for more information about the algorithm, particularly for efficiently moving around all the pointers.
Modifier and Type | Method and Description |
---|---|
static BigInteger |
countNumberOfCombinations(long n,
long r)
Calculates the number of combinations for 'n choose r'.
|
static <T> Iterator<T[]> |
iterator(T[] set)
Creates an Iterator that can be used to iterate over all possible
combinations of elements out of the supplied set.
|
static <T> Iterator<T[]> |
iterator(T[] set,
int size)
Creates an Iterator that can be used to iterate over all possible
combinations where each combination is of the supplied size.
|
public static BigInteger countNumberOfCombinations(long n, long r)
n
- Number of elements in the set.r
- Number of elements taken out of the set.public static <T> Iterator<T[]> iterator(T[] set)
T
- Type of a single element of a combination.set
- The set containing all the elements.IllegalArgumentException
- the supplied set does not contain
between 1 and MAX_SIZE_OF_SMALL_SET
elements.NullPointerException
- When the supplied set is null.public static <T> Iterator<T[]> iterator(T[] set, int size)
T
- Type of a single element of a combination.set
- The set containing all the elements.size
- Size each combination should have.
NullPointerException
- When the supplied set is null.IllegalArgumentException
- When the supplied size is not in the
range [0 .. set.length()]Copyright © 2008–2018. All rights reserved.