How can I concatenate two arrays in Java?

Antti Kissaniemi picture Antti Kissaniemi · Sep 17, 2008 · Viewed 1.1M times · Source

I need to concatenate two String arrays in Java.

void f(String[] first, String[] second) {
    String[] both = ???
}

What is the easiest way to do this?

Answer

Antti Kissaniemi picture Antti Kissaniemi · Sep 17, 2008

I found a one-line solution from the good old Apache Commons Lang library.
ArrayUtils.addAll(T[], T...)

Code:

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