@Override
        public int[] next() {
            if (curr == null)
                return null;
            final int last=lengths.length-1;
            if (curr[last] < lengths[last]-1) {
                int[] ret = curr.clone();
                curr[last]++;
                return ret;
            }
            if (curr[last] == lengths[last]-1) {
                // go left to search for more capacity
                for (int j=last-1; j >= 0; j--) {
                    if (curr[j] < lengths[j]-1) {
                        int[] ret = curr.clone();
                        curr[j]++;
                        for (int k=j+1; k < lengths.length; k++) {
                            curr[k] = 0;
                        }
                        return ret;
                    }
                }
                int[] ret = curr;   // last combination
                curr = null;
                return ret;
            }
            throw new IllegalStateException("Bug ?");
        }
Returns the next combination of array index values, or null if
the index value space has been exhausted.
aws-java-sdk-core/src/test/java/com/amazonaws/util/IndexValues.java