我想生成一个数组,其内容将表示两个给定字符串数组中的Cartesian product个.
换句话说,我需要将第一个数组arr1
中的每个String
与第二个数组arr2
中的每个String
连接起来.
Here is my code:个
String[] arr1 = {"a", "b", "c"};
String[] arr2 = {"d", "e", "f"};
String[] result = new String[arr1.length * arr2.length];
for (int k = 0; k < result.length; k++) {
for (int i = 0; i <arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
result[k] = arr1[i] + arr2[j];
}
}
}
System.out.println(Arrays.toString(result));
Current Output:个
[cf, cf, cf, cf, cf, cf, cf, cf, cf]
Desired Output:个
[ad, ae, af, bd, be, bf, cd, ce, cf]
我怎么才能修好它?