Java TimeUnit Usage getAllCombinations( ArrayList lengths)

Here you can find the source of getAllCombinations( ArrayList lengths)

Description

Given a vector of sizes, every possible combination consisting of one member from each groups of a given size is returned.

License

Creative Commons License

Parameter

Parameter Description
lengths size of each group

Return

all combinations

Declaration

public static ArrayList<ArrayList<Integer>> getAllCombinations(
        ArrayList<Integer> lengths) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class Main {
    /**//from   w w w.  j a  v  a2 s .  c  o m
     * Given a vector of sizes, every possible combination consisting of one
     * member from each groups of a given size is returned. The results are
     * returned in a vector, where each member is a vector that in turn contains
     * the indices in the group of each member in that combination.
     *
     * @param lengths size of each group
     * @return all combinations
     */
    public static ArrayList<ArrayList<Integer>> getAllCombinations(
            ArrayList<Integer> lengths) {
        long start = System.currentTimeMillis();
        ArrayList<ArrayList<Integer>> combos = new ArrayList<ArrayList<Integer>>();
        getAllCombinations(lengths, 0,
                new ArrayList<Integer>(lengths.size()), combos);
        System.out.println(combos.size() + " combinations of length "
                + combos.get(0).size());
        long end = System.currentTimeMillis();
        System.out.println("Combination computation time: "
                + TimeUnit.MILLISECONDS.toMinutes(end - start)
                + " minutes (" + (end - start) + " milliseconds)");
        return combos;
    }

    /**
     * Recursive helper method for getAllCombinations above
     *
     * @param lengths sizes of all groups
     * @param idx index in lengths
     * @param soFar collection of members currently being built
     * @param combos accumulates complete combinations
     */
    public static void getAllCombinations(final ArrayList<Integer> lengths,
            int idx, ArrayList<Integer> soFar,
            ArrayList<ArrayList<Integer>> combos) {
        if (idx == lengths.size()) {
            //System.out.println(soFar);
            combos.add((ArrayList<Integer>) soFar.clone());
        } else {
            int numOptions = lengths.get(idx);
            for (int i = 0; i < numOptions; i++) {
                soFar.add(i);
                getAllCombinations(lengths, idx + 1, soFar, combos);
                soFar.remove(soFar.size() - 1);
            }
        }
    }
}

Related

  1. formatUptime(long startTime)
  2. formatUptime(long uptimeInMs)
  3. generateRandomDateTimeForLogEvent(Date laterThan)
  4. getAge(Date birthday)
  5. getAge(long currentTimeMillis, long headerTimestampSec)
  6. getAllDaysCount(final long milliseconds)
  7. getBookingDays(Calendar start, Calendar end)
  8. getCurrentDateTime(int offsetMin)
  9. getCurrentTimeMillis()