#### In java, how to concatenate two arrays

Total Post:24

Points:168
2422  View(s)
Ratings:
Rate this:
Need to concatenate two String arrays in Java.
`void f(String[] first, String[] second) {    String[] both = ???}`
What is the easiest way to do this?
1. Post:397

Points:3117
##### Re: In java, how to concatenate two arrays

Solution 1:

Use Apache Commons Lang library

Solution 2:
`public Foo[] concat(Foo[] a, Foo[] b) {   int aLen = a.length;   int bLen = b.length;   Foo[] c= new Foo[aLen+bLen];   System.arraycopy(a, 0, c, 0, aLen);   System.arraycopy(b, 0, c, aLen, bLen);   return c;}`

Here is a version that works with generics:

`public <T> T[] concatenate (T[] a, T[] b) {    int aLen = a.length;    int bLen = b.length;    @SuppressWarnings("unchecked")    T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen+bLen);    System.arraycopy(a, 0, c, 0, aLen);    System.arraycopy(b, 0, c, aLen, bLen);    return c;}`
Note like all generics it will not work with primitives but with Objects.

Solution 3:
use System.arraycopy() to ensure that copying large arrays is as fast as possible.

For two arrays it looks like this:

`public static <T> T[] concat(T[] first, T[] second) {  T[] result = Arrays.copyOf(first, first.length + second.length);  System.arraycopy(second, 0, result, first.length, second.length);  return result;}`
And for a arbitrary number of arrays (>= 1) it looks like this:

`public static <T> T[] concatAll(T[] first, T[]... rest) {  int totalLength = first.length;  for (T[] array : rest) {    totalLength += array.length;  }  T[] result = Arrays.copyOf(first, totalLength);  int offset = first.length;  for (T[] array : rest) {    System.arraycopy(array, 0, result, offset, array.length);    offset += array.length;  }  return result;}`

Solution 4:
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);`

Solution 5:
Or with the beloved Guava:
String[] both = ObjectArrays.concat(first, second, String.class);

Using the Java API:

Solution 6:
`String[] f(String[] first, String[] second) {    List<String> both = new ArrayList<String>(first.length + second.length);    Collections.addAll(both, first);    Collections.addAll(both, second);    return both.toArray(new String[both.size()]);}`

Solution 7:
without System.arraycopy (not available in GWT client for example):

`static String[] concat(String[]... arrays) {    int lengh = 0;    for (String[] array : arrays) {        lengh += array.length;    }    String[] result = new String[lengh];    int pos = 0;    for (String[] array : arrays) {        for (String element : array) {            result[pos] = element;            pos++;        }    }    return result;}`

Solution 8:
The Functional Java library has an array wrapper class that equips arrays with handy methods like concatenation.

import static fj.data.Array.array;
...and then

Array<String> both = array(first).append(array(second));
To get the unwrapped array back out, call

String[] s = both.array();

Solution 9:
ArrayList<String> both = new ArrayList(Arrays.asList(first));

both.toArray();

Modified On Apr-10-2018 12:58:31 AM

#### CONTACT INFO

10-B Taskand Marg, Near Patrika Chauraha, Civil Lines, Prayagraj, UP, India-211001.
contact@mindstick.com
91-532-2400505

969-G Edgewater Blvd,Suite
793 Foster City-94404, CA (USA)
+1-650-242-0133