Home > DeveloperSection > Forums > In java, how to concatenate two arrays
Felix Pickles
Felix Pickles

Total Post:24

Points:168
Posted on    July-01-2015 2:04 AM

 Java Java  Array 
Ratings:


 1 Reply(s)
 890  View(s)
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?


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    July-01-2015 4:55 AM

Solution 1:
Use Apache Commons Lang library
String[] both = ArrayUtils.addAll(first, second);

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.addAll(Arrays.asList(second));

both.toArray();

Don't want to miss updates? Please click the below button!

Follow MindStick