-19

I have two arrays:

a = ["a","b","c"]

b = ["d","e","f"]

How can I merge them into a single array, like so:

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

using the equals sign (=) as a delimiter between the merged strings?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
andi2.2
  • 86
  • 1
  • 11
  • 7
    This question is being discussed on [meta](https://meta.stackoverflow.com/questions/405053/should-a-question-without-source-code-in-it-be-closed). – Sabito stands with Ukraine Feb 09 '21 at 08:06
  • 8
    Wrong duplicate because `ArrayList` is not same as an `array` – anubhava Mar 21 '21 at 07:21
  • 4
    The technique is the same, iterate, concat, add. – Wiktor Stribiżew Mar 21 '21 at 13:00
  • 4
    Nope. `ArrayList` has built-in `.addAll` method to do so, no need to iterate. – anubhava Mar 21 '21 at 16:32
  • 2
    For anyone with a non-Java background, a simple google search like "what are the differences between array and ArrayList in java" can bring some basic differences. On top of that, there are tens of differences between them in terms of API. – Arvind Kumar Avinash Mar 22 '21 at 10:01
  • 3
    `addAll` isn't relevant since you need to add the custom delimiter. Aniway this question isn't specific enough : how do we want to handle size difference in arrays ? `Not using for loop`, do we exclude also while loop ? gotos ? And why are we excluding for loop for something like that in the first place ? Also the chosen answer doesn't fill the criteria "without for loop". If this is a homework question, what was the subjet of the class ? Streams ? – Walfrat Mar 31 '21 at 14:21
  • 3
    This question is perfectly clear. Presumably, @andi2.2 is wondering if there is a way to do this using Java 8 Streams, similarly to the python `[f'{a_el}={b_el}' for a_el, b_el in zip(a,b)]` – Gavin S. Yancey Mar 31 '21 at 22:19
  • Using Guava Streams, the answer is `Streams.zip(Arrays.stream(a), Arrays.stream(b), (aEl, bEl) -> aEl + "=" + bEl).toArray(String[]::new);` – Gavin S. Yancey Mar 31 '21 at 22:21
  • Using just the standard library, there is a less-elegant but still working way: `IntStream.range(0, Math.min(a.length, b.length)).mapToObj(i -> a[i] + "=" + b[i]).toArray(String[]::new);` -- EDIT: just noticed the one answer here mentions this. – Gavin S. Yancey Mar 31 '21 at 22:24
  • Could *potentially* be closed as a duplicate of , but IMO should be left open so that the answer here can tell OP what they want is to `zip` the steams, and how to use that to do what they want. – Gavin S. Yancey Mar 31 '21 at 22:27
  • 1
    @Walfrat The second part of the chosen answer does not use a `for` loop. – Unmitigated Apr 01 '21 at 15:18

1 Answers1

6

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));
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110