Class CombinationIterator<T>

  • Type Parameters:
    T -
    All Implemented Interfaces:
    Iterator<List<T>>


    public class CombinationIterator<T>
    extends Object
    implements Iterator<List<T>>
    This iterates through every combination of a list of arrays. For example:
    List<String[]> input = new ArrayList<>();
    input.add(new String[] {"1", "2", "3"});
    input.add(new String[] {"apple", "orange"});
    input.add(new String[] {"X"});
    CombinationIterator<String> iter = new CombinationIterator<>(input);
    while(iter.hasNext()) {
            System.out.println(iter.next());
    }

    This code will output this list:

    • "1", "apple", "X"
    • "1", "orange", "X"
    • "2", "apple", "X"
    • "2", "orange", "X"
    • "3", "apple", "X"
    • "3", "orange", "X"

    It helps to visualize this as a slot machine, where "1", "2", "3" are vertically stacked as one place/slot in the machine. "apple", "orange" is the next slot, and "X" is always the 3rd slot.

    • Constructor Detail

      • CombinationIterator

        public CombinationIterator(T[]... list)
      • CombinationIterator

        public CombinationIterator(List<T[]> list)
    • Method Detail

      • hasNext

        public boolean hasNext()
      • next

        public List<T> next()
      • incrementPositions

        protected void incrementPositions()
      • remove

        public void remove()