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?
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?
If you use this way so you no need to import any third party class.
If you want concatenate
String
Sample code for concate two String Array
If you want concatenate
Int
Sample code for concate two Integer Array
Here is Main method
We can use this way also.
A simple variation allowing the joining of more than one array:
Using the Java API:
You can append the two arrays in two lines of code.
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 as these are not efficient. The built in methods use block-copy functions that are extremely fast.How about :
An easy, but inefficient, way to do this (generics not included):