[Solved] Merge two one dimensional String arrays to a single array with delimiter


You can do it with the help of a loop e.g.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] a = { "a", "b", "c" };
        String[] b = { "d", "e", "f" };
        String[] result = new String[a.length];
        for (int i = 0; i < Math.min(a.length, b.length); i++) {
            result[i] = a[i] + "=" + b[i];
        }

        System.out.println(Arrays.toString(result));
    }
}

Output:

[a=d, b=e, c=f]

Learn more about loops at this tutorial from Oracle.

Using IntStream:

import java.util.Arrays;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        String[] a = { "a", "b", "c" };
        String[] b = { "d", "e", "f" };
        String[] result = IntStream
                            .range(0, Math.min(a.length, b.length))
                            .mapToObj(i -> a[i] + "=" + b[i])
                            .toArray(String[]::new);
        
        System.out.println(Arrays.toString(result));
    }
}

solved Merge two one dimensional String arrays to a single array with delimiter