0 votes
1 view
in Java by (2.6k points)

What is the easiest way to concatenate two arrays in JAVA?

void f(String[] first, String[] second)


    String[] both = ???


1 Answer

0 votes
by (34.9k points)

To concatenate two arrays in JAVA either use this one linear from old Apache Commons Lang Library:

ArrayUtils.addAll(T[], T...)


String[] both = ArrayUtils.addAll(first, second);

Or use Stream in Java 8:

String[] both = Stream.concat(Arrays.stream(a), Arrays.stream(b)) .toArray(String[]::new);


String[] both = Stream.of(a, b).flatMap(Stream::of) .toArray(String[]::new);