简体   繁体   English

通过添加第一个和第二个 Arrays 的元素来填充第三个数组

[英]Fill the Third Array by adding the elements of the First and Second Arrays

I want to generate an array which content would represent a Cartesian product of the two given string-arrays.我想生成一个数组,其内容将代表两个给定字符串数组的笛卡尔积

In other words, I need to concatenate each String from the first array arr1 with every String from the second array arr2 .换句话说,我需要将第一个数组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:当前 Output:

[cf, cf, cf, cf, cf, cf, cf, cf, cf]

Desired Output:所需的 Output:

[ad, ae, af, bd, be, bf, cd, ce, cf]

How can I fix it?我该如何解决?

You don't need the very first for -loop, it causes all the values in the resulting array to be overwritten multiple times.您不需要第一个for循环,它会导致结果数组中的所有值被多次覆盖。

Instead, you need to declare the index k of the resulting array outside the nested loop that generates the Cartesian product of the two given arrays.相反,您需要在生成两个给定 arrays 的笛卡尔积的嵌套循环之外声明结果数组的索引k k should be incremented at each iteration step of the inner loop. k应该在内循环的每个迭代步骤中递增。

int k = 0;
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(arr3));

Output: Output:

[ad, ae, af, bd, be, bf, cd, ce, cf]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:使用第一个数组的前3个整数,然后是第二个数组的3个整数,将2个数组组合成第三个数组 - Java: Combine 2 arrays into a third array using the first 3 integers of the first array then 3 integers from the second one 如何打印第一个数组中的单词,第二个数组中的第二个和第三个数组中的第三个的单词组合? - How to print a combination of words one from first array the second from second array and third from third array? Java循环收集数组中每三个元素的第二个和第三个元素 - Java loop to collect the second and third elements every three in an array 用户提供的整数数组的第一,第二和第三大数 - First, Second and Third largest number from user provided integer array 将两个数组求和到第三个数组 - summing 2 arrays to third array Java比较两个数组并检查第一个数组是否与第二个数组连续 - Java comparing two arrays and checking if the First array is consecutive of the Second 给定2个数组,如何按升序对第一个数组进行排序,并交换第二个数组的值以便与第一个数组匹配? - Given 2 arrays, how to sort the first array in ascending order, and swap the second array values so as to match with the first array? Java List:当我调用Arrays.fill()来填充数组时,为什么它会出现一些意想不到的元素? - Java List: When I call Arrays.fill() to fill an array, why it will appear some unexpected elements? 创建两个 arrays 可以存储一个人的名字(在第一个数组中)和姓氏(在第二个数组中) - Create two arrays that can store the first name (in first array) and last name (in second array) of a person 元素的总和到第三个数组 - Array sum of elements to third array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM