|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.bezier.math.combinatorics.CombinatoricsBase
de.bezier.math.combinatorics.Combination
public class Combination
Unique combinations, no duplications, order is ignored
This class represents sets that have no duplicate elements (no "a a") and order is ignored ("a b" equals "b a" and is therefore counted only once).
Given the elements [a, b, c] Combination will return these results, for length (of a single result, an array) 0: [] length 1: [a], [b], [c] length 2: [a, b], [a, c], [b, c] length 3: [a, b, c]
Constructor Summary | |
---|---|
Combination(int elements)
Same as calling Combination( elements, elements ) |
|
Combination(int elements,
int length)
As with all classes of package de.bezier.math.combinatorics this will return arrays of indices for you to use on whatever you want. |
Method Summary | |
---|---|
boolean |
hasMore()
Has more results? Any left to be read? |
int[] |
next()
Return current result (an array of indices, see explaination at constructor) and go to next result (if possible) |
void |
rewind()
Rewind, start over. |
Methods inherited from class de.bezier.math.combinatorics.CombinatoricsBase |
---|
nextAndStep, numberOfElements, position, positionAsInt, positionAsLong, positionInPercent, positionInPercent, total, totalAsInt, totalAsLong |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Combination(int elements)
public Combination(int elements, int length)
As with all classes of package de.bezier.math.combinatorics this will return arrays of indices for you to use on whatever you want.
Say you have an array like this {"a", "b", "c"} and want all possible pairs of combinations, you could do:
// your array .. String[] yourArray = new String[]{"a", "b", "c"}; // tell Combination the length of your array and how long results should be: // 3 elements has your array (yourArray.length), results should have a length of 2 Combination combi = new Combination( yourArray.length, 2 );
This will give you access to the following 3 results:
{0,1}, {0,2}, {1,2}
Which you can then use to read from your original array:
int[] result = combi.next(); // use contents of result as indices (addresses) into your array: // yourArray[result[0]] will give "a" since it would be same as writing: "yourArray[0]" // yourArray[result[1]] will give "b" result = combi.next(); // yourArray[result[0]] will give "a" // yourArray[result[1]] will give "c" result = combi.next(); // yourArray[result[0]] will give "b" // yourArray[result[1]] will give "c"
Since there can be no duplications of elements Combination will not take a higher length than the given number of elements.
elements
- Number of elements to use as input, can not be negative ( elements > 0 )length
- The length of the results, can not be higher than elements and not be negative ( length >= elements, length > 0 )Method Detail |
---|
public void rewind()
rewind
in class CombinatoricsBase
public boolean hasMore()
// use this as condition: while ( lottoNumbers.hasMore() ) { int[] numbersToPlay = lottoNumbers.next(); // ... go buy a ticket for each result and you will win it all! }
hasMore
in class CombinatoricsBase
public int[] next()
next
in class CombinatoricsBase
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |