import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class PerformCartesianProductOfTwoArrays_Java {
public static int[][] cartesianProduct(int[] arr1, int[] arr2) {
List<int[]> list = new ArrayList<>();
for (int val1 : arr1) {
for (int val2 : arr2) {
list.add(new int[]{val1, val2});
}
}
int[][] result = new int[list.size()][2];
int i = 0;
for (int[] val : list) {
result[i++] = val;
}
return result;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {6, 7, 8, 9};
System.out.println("Cartesian product:\n" + Arrays.deepToString(cartesianProduct(arr1, arr2)));
}
}
/*
run:
Cartesian product:
[[1, 6], [1, 7], [1, 8], [1, 9], [2, 6], [2, 7], [2, 8], [2, 9], [3, 6], [3, 7], [3, 8], [3, 9]]
*/