com.objectwave.utility
Class Permutations

java.lang.Object
  |
  +--com.objectwave.utility.Permutations
All Implemented Interfaces:
java.util.Enumeration

public class Permutations
extends java.lang.Object
implements java.util.Enumeration

The Permutations class provides an enumeration of all permutations of an array of objects. Each “permutation” is simply an ordered list of the group.

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::

     Object[] children = {“Leonardo”, “Monica”, “Nathan”, “Olivia”};
 
To see all 2-permutations of these 4 names, create and use a Permutations enumeration:
 Permutations c = new Permutations(children, 2);
 while (c.hasMoreElements()) {
  Object[] perm = (Object[])c.nextElement();
  for (int i = 0; i < perm.length; i++) {
    System.out.print(perm[i] + “ “);
  }
 System.out.println();
 }
 
This will print out:
 
 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 Summary
Permutations(java.lang.Object[] inArray)
          Create a Permutation to enumerate through all possible lineups of the supplied array of Objects.
Permutations(java.lang.Object[] inArray, int m)
          Create a Permutation to enumerate through all possible lineups of the supplied array of Objects.
 
Method Summary
 boolean hasMoreElements()
           
 java.lang.Object nextElement()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Permutations

public Permutations(java.lang.Object[] inArray)
             throws CombinatoricException
Create a Permutation to enumerate through all possible lineups of the supplied array of Objects.

Throws:
CombinatoricException - Should never happen with this interface

Permutations

public Permutations(java.lang.Object[] inArray,
                    int m)
             throws CombinatoricException
Create a Permutation to enumerate through all possible lineups of the supplied array of Objects.

Parameters:
inArray - java.lang.Object[], the group to line up
m - int, the number of objects to use
Throws:
CombinatoricException - if m is greater than the length of inArray, or less than 0.
Method Detail

hasMoreElements

public boolean hasMoreElements()
Specified by:
hasMoreElements in interface java.util.Enumeration
Returns:
true, unless we have already returned the last permutation.

nextElement

public java.lang.Object nextElement()
Specified by:
nextElement in interface java.util.Enumeration
Returns:
java.lang.Object, the next permutation of the original Object array.

Actually, an array of Objects is returned. The declaration must say just “Object,” because the Permutations class implements Enumeration, which declares that the “nextElement()” returns a plain Object. Users must cast the returned object to (Object[]).