common
Class PermutationIterator

java.lang.Object
  extended by common.PermutationIterator
All Implemented Interfaces:
java.util.Iterator

public class PermutationIterator
extends java.lang.Object
implements java.util.Iterator

An iterator for iterating over all the ways to make an ordered list of k distinct elements from a set of size n. When k = n, this means iterating over all the permutations of a set of size n. We assume the set of size n can be ordered in some way, so that its elements are numbered 0, 1, ..., n-1. A permutation is represented as an integer array of length k containing numbers in {0, 1, ..., n-1}. Each number occurs at most once in the permutation.


Constructor Summary
PermutationIterator(int n, int k)
          Creates a new PermutationIterator for iterating over permutations of k elements from a set of size n.
 
Method Summary
 boolean hasNext()
          Returns true if there is a permutation that hasn't been returned yet.
static void main(java.lang.String[] args)
           
 java.lang.Object next()
          Returns the next permutation.
 void remove()
          Not supported.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PermutationIterator

public PermutationIterator(int n,
                           int k)
Creates a new PermutationIterator for iterating over permutations of k elements from a set of size n.

Method Detail

hasNext

public boolean hasNext()
Returns true if there is a permutation that hasn't been returned yet.

Specified by:
hasNext in interface java.util.Iterator

next

public java.lang.Object next()
Returns the next permutation. Note that this modifies the int[] that was returned by the previous call to next.

Specified by:
next in interface java.util.Iterator
Returns:
an int[] representing a permutation that has not already been returned

remove

public void remove()
Not supported.

Specified by:
remove in interface java.util.Iterator

main

public static void main(java.lang.String[] args)