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 (41k points)

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

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

example:

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);

Or:

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

Related questions

+1 vote
2 answers
+2 votes
3 answers
0 votes
2 answers
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...