public class Permutations<E>
extends java.lang.Object
implements java.util.Iterator<java.util.List<E>>
For example, to see all of the ways we can select a school representative and an alternate from a list of 4 children, begin with an array of names::
To see all 2-permutations of these 4 names, create and use a Permutations enumeration:Listchildren = Collections.asList({Leonardo, Monica, Nathan, Olivia});
This will print out:Permutationsc = new Permutations (children, 2); while (c.hasNext()) { List perm = c.next(); for (int i = 0; i < perm.size(); i++) { System.out.print(perm.get(i) + ); } System.out.println(); }
Leonardo Monica Leonardo Nathan Leonardo Olivia Monica Leonardo Monica Nathan Monica Olivia Nathan Leonardo Nathan Monica Nathan Olivia Olivia Leonardo Olivia Monica Olivia Nathan
Constructor and Description |
---|
Permutations(java.util.List<E> inList)
Create a Permutation to iterate through all possible lineups
of the supplied array of Objects.
|
Permutations(java.util.List<E> inList,
int m)
Create a Permutation to iterate through all possible lineups
of the supplied array of Objects.
|
Modifier and Type | Method and Description |
---|---|
boolean |
hasNext() |
java.util.List<E> |
next() |
void |
remove() |
public Permutations(java.util.List<E> inList)
Object[]
- inArray the group to line upCombinatoricException
- Should never happen
with this interfacepublic Permutations(java.util.List<E> inList, int m)
Object[]
- inArray the group to line upinArray
- java.lang.Object[], the group to line upm
- int, the number of objects to useCombinatoricException
- if m is greater than
the length of inArray, or less than 0.public boolean hasNext()
hasNext
in interface java.util.Iterator<java.util.List<E>>
public java.util.List<E> next()
next
in interface java.util.Iterator<java.util.List<E>>
Actually, an array of Objects is returned. The declaration must say just Object, because the Permutations class implements Iterator, which declares that the next() returns a plain Object. Users must cast the returned object to (Object[]).
public void remove()
remove
in interface java.util.Iterator<java.util.List<E>>