Share this page 

Merge (or add) two arrays into oneTag(s): Language


The easy way is to convert the arrays into Lists, add one into another and finally convert back to an array.

import java.util.*;

public class Test2 {
  public static void main(String args[]){
    String a[] = {"a", "b", "c"};
    String b[] = {"d", "e" };

    List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));
    Object [] c = list.toArray();
    System.out.println(Arrays.toString(c));
    /*
     output :  [a, b, c, d, e]
    */
  }
}

From there a more versatile method is developped to accept a list of arrays to be merged into one.

public class CollectionUtils {
  private CollectionUtils () {}

  public static String[] join(String [] ... parms) {
    // calculate size of target array
    int size = 0;
    for (String[] array : parms) {
      size += array.length;
    }

    String[] result = new String[size];

    int j = 0;
    for (String[] array : parms) {
      for (String s : array) {
        result[j++] = s;
      }
    }
    return result;
  }

  public static void main(String[] args) {
    String a[] = { "1", "2", "3" };
    String b[] = { "4", "5", "6" };
    String c[] = { "7", "8", "9" };

    String[] big = (String [])join(a,b,c);
    System.out.println(java.util.Arrays.toString(big));
    /*
     * output :
     *    [1, 2, 3, 4, 5, 6, 7, 8, 9]
     */
  }
}