Im trying to write a function to display all combinations in a jagged array, where each combination contains one element from each sub-array. The jagged array can consist of any number of arrays and each array can have any number of elements. E.g. for the following array: a[0] = {1, 3, 5} a[1] = {2, 4} it should return: (1, 2) (1, 4) (3, 2) (3, 4) (5, 2) (5, 4)
I thought of doing it this way but immediately run into trouble. Logically it looks OK to get 1, 2 and 1, 4 but then next run I is set back to 0 (sorry not at devel machine to test now). Can anyone suggest a better solution please?
Here is my code
for (int i = 0; i < array1.length(); i++)
for (int j = 0; j < array2.length(); j++)
if (j < array2.length())
i = 0;
else
i++;
System.out.println(array1[i] "," array2[j])