Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a combination iterator I wrote recetnly</p> <pre><code>package psychicpoker; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import static com.google.common.base.Preconditions.checkArgument; public class CombinationIterator&lt;T&gt; implements Iterator&lt;Collection&lt;T&gt;&gt; { private int[] indices; private List&lt;T&gt; elements; private boolean hasNext = true; public CombinationIterator(List&lt;T&gt; elements, int k) throws IllegalArgumentException { checkArgument(k&lt;=elements.size(), "Impossible to select %d elements from hand of size %d", k, elements.size()); this.indices = new int[k]; for(int i=0; i&lt;k; i++) indices[i] = k-1-i; this.elements = elements; } public boolean hasNext() { return hasNext; } private int inc(int[] indices, int maxIndex, int depth) throws IllegalStateException { if(depth == indices.length) { throw new IllegalStateException("The End"); } if(indices[depth] &lt; maxIndex) { indices[depth] = indices[depth]+1; } else { indices[depth] = inc(indices, maxIndex-1, depth+1)+1; } return indices[depth]; } private boolean inc() { try { inc(indices, elements.size() - 1, 0); return true; } catch (IllegalStateException e) { return false; } } public Collection&lt;T&gt; next() { Collection&lt;T&gt; result = new ArrayList&lt;T&gt;(indices.length); for(int i=indices.length-1; i&gt;=0; i--) { result.add(elements.get(indices[i])); } hasNext = inc(); return result; } public void remove() { throw new UnsupportedOperationException(); } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload