How is it possible to do the Cartesian product using two arrays?
A={1,2,3}
B={2,3,4}
C={(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,2),(3,3),(3,4)}
This is the code I was using, but I always get a IndexOutOfBoundException
.
public int[] cartesianProduct(int[] s1, int[] s2) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < s1.length; i++) {
for (int v1 : s1) {
for (int v2 : s2) {
list.add(s1[i], s2[i]);
}
}
}
int[] result = new int[list.size()];
int k = 0;
for (int i : list) {
result[k++] = i;
}
return result;
}