How to create cartesian product over arbitrary groups of numbers in Java?

后端 未结 4 1336
野性不改
野性不改 2021-02-08 07:36

Let\'s say I have 2 groups of numbers:

{1, 2, 3},
{4, 5}

I\'d like to create an algorithm (in Java) that outputs the following 6 combinations:<

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-08 08:08

    If you can use libraries, Guava's Sets.cartesianProduct(List>) does exactly what you're looking for. (Disclosure: I contribute to Guava.)

提交回复
热议问题