I need to concatenate two String
arrays in Java.
void f(String[] first, String[] second) {
String[] both = ???
}
What is t
You can append the two arrays in two lines of code.
String[] both = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, both, first.length, second.length);
This is a fast and efficient solution and will work for primitive types as well as the two methods involved are overloaded.
You should avoid solutions involving ArrayLists, streams, etc as these will need to allocate temporary memory for no useful purpose.
You should avoid for
loops for large arrays as these are not efficient. The built in methods use block-copy functions that are extremely fast.