public class Combinations<E> extends Object implements Enumeration<List<E>>
Objects
, along with the number to select.
For example, to choose 3 boys from a list of 5, begin with an array of names:
String[] boys = {"Alfred", "Ben", "Carl", "Drew", "Edwin"};
To see all combinations of these 5 names taken 3 at a time, create
and use a Combinations
enumeration:
Combinations<String> c = new Combinations<String>(boys, 3); while (c.hasMoreElements()) { List<String> combo = c.nextElement(); System.out.println(combo); }
This will print out a 10 line list:
Alfred Ben Carl Alfred Ben Drew Alfred Ben Edwin Alfred Carl Drew Alfred Carl Edwin Alfred Drew Edwin Ben Carl Drew Ben Carl Edwin Ben Drew Edwin Carl Drew Edwin
Constructor and Description |
---|
Combinations(E[] inArray,
int m)
Creates a combination to enumerate through all subsets of the
supplied array, selecting
m items at a time. |
public Combinations(E[] inArray, int m)
m
items at a time.inArray
- the group to choose fromm
- the number to select in each choicepublic boolean hasMoreElements()
hasMoreElements
in interface Enumeration<List<E>>
true
if there is at least one combination that has not been fetched yetpublic List<E> nextElement()
nextElement
in interface Enumeration<List<E>>
m
elements, or null
all possible combinations have been returnedpublic static void main(String[] args)
args
- no argument required