I want to create a Java method, which accepts an inputArray = Object[n][]
, where n can be any integer, and outputs a list of possible n-size combinations between all values of the n subarrays. Below is an example:
Input Array: (where Object=String and n=3)
String[] subarrayA = {"A0","A1","A2"};
String[] subarrayB = {"B0","B1"};
String[] subarrayC = {"C0","C1","C2","C3"};
String[3][] inputArray = {subarrayA, subarrayB, subarrayC};
Desired Output:
{A0,B0,C0},{A0,B0,C1},{A0,B0,C2},{A0,B0,C3},
{A0,B1,C0},{A0,B1,C1},{A0,B1,C2},{A0,B1,C3},
{A1,B0,C0},{A1,B0,C1},{A0,B0,C2},{A1,B0,C3},
{A1,B1,C0},{A1,B1,C1},{A1,B1,C2},{A1,B1,C3},
{A2,B0,C0},{A2,B0,C1},{A2,B0,C2},{A2,B0,C3},
{A2,B1,C0},{A2,B1,C1},{A2,B1,C2},{A2,B1,C3}
Obviously, I cannot have a fixed nested-loop inside my method since I do not know n
in advance. So, I am guessing the only way to solve it would be through a recursive method? Any recommendations?
P.S: I am aware of the simple combination-related posts on the website.