fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<List<String>> combList = new ArrayList<>();
  13. combList.add(Arrays.asList("a1", "a2", "a3"));
  14. combList.add(Arrays.asList("b1", "b2"));
  15. combList.add(Arrays.asList());
  16. combList.add(Arrays.asList("c1", "c2", "c3"));
  17.  
  18. combinations(combList, 0, 0, "");
  19. }
  20.  
  21. public static void combinations(List<List<String>> combList, int listIndex, int itemIndex, String result)
  22. {
  23. // Am I at the bottom of the y-axis?
  24. if(listIndex < combList.size())
  25. {
  26. //Am I at the bottom of the x-axis?
  27. if(itemIndex < combList.get(listIndex).size())
  28. {
  29. List<String> curList = combList.get(listIndex);
  30. StringBuilder sb = new StringBuilder();
  31. sb.append(result).append(curList.get(itemIndex)).append(" ");
  32. combinations(combList, listIndex + 1, 0, sb.toString());
  33. combinations(combList, listIndex, itemIndex + 1, result);
  34. }else if (combList.get(listIndex).isEmpty()){
  35. combinations(combList, listIndex + 1, 0, result);
  36. }
  37. return;
  38. }
  39. System.out.println(result);
  40. //return; - redundant as last instruction of method
  41. }
  42. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
a1 b1 c1 
a1 b1 c2 
a1 b1 c3 
a1 b2 c1 
a1 b2 c2 
a1 b2 c3 
a2 b1 c1 
a2 b1 c2 
a2 b1 c3 
a2 b2 c1 
a2 b2 c2 
a2 b2 c3 
a3 b1 c1 
a3 b1 c2 
a3 b1 c3 
a3 b2 c1 
a3 b2 c2 
a3 b2 c3