0

I want to cycle through/access all possible combinations of the elements in a series of Lists in a clock-like fashion, but I will not know how many Lists there will be until runtime (i.e. I can't hardcode it). Normally, if I did know how many Lists there would be, I would use nested For loops in something like this:

for(int x = 0; x < total1; x++){
    for(int y = 0; y < total2; y++){
        for(int z = 0; z < total3; z++){
            //Handling code
        }
    }
}

Where the outermost loop represents the last List, and the innermost represents the first. However, because I do not know how many Lists there will be, I can't use this method, because I wouldn't know how many loops to use. Some things on the Internet said to use a recursive program that would repeatedly run for-loops, but I don't understand how this could reach the same result as the above method.

Is there a way to accomplish the same ends as the For-loops (accessing every combination of elements from the Lists, taking one element from each List) without knowing how many Lists/Loops are needed?

Edit: As an example, if I have 3 Lists, each with 4 elements:

[1,3,6,5], [0,5,8,2], [9,7,1,6]

Then my output should be, essentially, 64 Lists containing all possible examples, e.g.:

[1,0,9], [3,0,9], [6,0,9], [5,0,9],
[1,5,9], [3,5,9], [6,5,9], [5,5,9],
[1,8,9], [3,8,9], [6,8,9], [5,8,9],
[1,2,9], [3,2,9], [6,2,9], [5,2,9],
[1,0,7], [3,0,7], [6,0,7], [5,0,7] //...etc

Hope this helps!

TWG
  • 17
  • 3
  • 1
    That's typically when a while loop would be used, no? – Jonathan Hall Jul 24 '18 at 21:26
  • 2
    You should put this in context. It's hard to tell as it is currently written. – lealceldeiro Jul 24 '18 at 21:33
  • 1
    Depending on size of items in array and the necessity for optimal performance, recursion could be a consideration here. – Spencer D Jul 24 '18 at 21:35
  • To get this straight: You want to build the cartesian product of n lists where n is unknown at compile time? – Björn Zurmaar Jul 24 '18 at 21:35
  • 2
    If it's acceptable to use [Guava](https://github.com/google/guava) in your app, [`Lists.cartesianProduct`](https://google.github.io/guava/releases/snapshot-jre/api/docs/com/google/common/collect/Lists.html#cartesianProduct-java.util.List-) does exactly what you seem to be trying to achieve. – sumitsu Jul 24 '18 at 21:38
  • I believe this a task is where recursion is afoot. – scrutari Jul 24 '18 at 21:42
  • You need a stack to accomplish what you need and a backtracking algorithm. – rhobincu Jul 24 '18 at 21:59
  • @BjörnZurmaar Yes exactly, but I was unfamiliar with the term "cartesian product" until I searched for it just now – TWG Jul 24 '18 at 22:38
  • @sumitsu I'm fairly inexperienced with Guava but the code you linked in your comment looks perfect, I'll try it now and update if it works! – TWG Jul 24 '18 at 22:50
  • You might find [Cartesian product of arbitrary sets in Java](https://stackoverflow.com/questions/714108/cartesian-product-of-arbitrary-sets-in-java) helpful. – StaticBeagle Jul 24 '18 at 23:12

0 Answers0