Oct 13

The following utility method will allow you to merge any number of arrays.


    public static <T> T[] arrayMerge(T[]... arrays)
    {
        // Determine required size of new array

        int count = 0;
        for (T[] array : arrays)
        {
            count += array.length;
        }

        // create new array of required class

        T[] mergedArray = (T[]) Array.newInstance(
           arrays[0][0].getClass(),count);

        // Merge each array into new array

        int start = 0;
        for (T[] array : arrays)
        {
            System.arraycopy(array, 0, 
               mergedArray, start, array.length);
            start += array.length;
        }
        return (T[]) mergedArray;
    } 

written by objects \\ tags: , ,


11 Responses to “How can I merge two Java arrays into one combined array?”

  1. Kamran Says:

    Here is another variant…

    public static T[] merge(T[]… arrays) {
    List list = new ArrayList();

    for( T[] array : arrays )
    list.addAll( Arrays.asList( array ) );

    return (T[]) Array.newInstance( arrays[0][0].getClass(), list.size() );
    }

  2. Rarish Ravi Says:

    public static void merge(String[] a, String[] b)
    {
    List lst = new ArrayList(Arrays.asList(a));
    lst.addAll(Arrays.asList(b));

    Object[] result = lst.toArray();

    System.out.println(“Merged output result is “+Arrays.toString(result));
    }

  3. abc Says:

    hi

  4. def Says:

    yo

  5. Ganesh Says:

    public class array1 {
    public static void merge(String[] a, String[] b)
    {
    List lst = new ArrayList(Arrays.asList(a));
    lst.addAll(Arrays.asList(b));

    Object[] result = lst.toArray();
    Arrays.sort(result);

    System.out.println(“Merged output result is”+Arrays.toString(result));
    }

    public static void main(String arg[]){
    String a[]={“1″,”2″,”3″,”4″,”5″};
    String b[]={“2″,”3″,”1″,”4″,”5″};
    array1.merge(a, b);

    }
    }

  6. Emanuele Says:

    All the variants posted here in the comments are pointless, unefficient and, most of all, get rid of the extremely efficient System.arraycopy.

  7. Fmanuele Says:

    @Emanuele why dont you tell us your genius-blessed own methods?

  8. Emanuele Says:

    @Fmanuele: my favourite version is the one posted into the article. I was going to design my own implementation using System.arraycopy, in a similar fashion to this article, when I came across to it. I just simply adapted to my purposes, since I found it very clever.

  9. Nicolas Dos Santos Says:

    This versions require Java 6, as they use Arrays.copyOf()

    public static T[] concat(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;
    }

  10. austin Says:

    Thanks! =)

  11. rohit kasat Says:

    for o(n) complexity 3 array merge

    public static ArrayList merge(int al[], int bl[], int cl[]) {
    ArrayList tl = new ArrayList();
    int ai, bi, ci;
    ai = bi = ci = 0;

    while (true) {
    if (ai != al.length) {
    if (bi != bl.length) {
    if (al[ai] < bl[bi]) {
    if (ci != cl.length) {
    if (al[ai] < cl[ci]) {
    tl.add(al[ai]);
    ai++;
    } else {
    tl.add(cl[ci]);
    ci++;
    }
    } else {
    tl.add(al[ai]);
    ai++;
    }
    } else {
    if (ci != cl.length) {
    if (bl[bi] < cl[ci]) {
    tl.add(bl[bi]);
    bi++;
    } else {
    tl.add(cl[ci]);
    ci++;
    }
    } else {
    tl.add(bl[bi]);
    bi++;
    }

    }
    } else {
    if (ci != cl.length) {
    if (al[ai] < cl[ci]) {
    tl.add(al[ai]);
    ai++;
    } else {
    tl.add(cl[ci]);
    ci++;
    }
    } else {
    tl.add(al[ai]);
    ai++;
    }
    }
    } else if (bi != bl.length) {
    if (ci != cl.length) {
    if (bl[bi] < cl[ci]) {
    tl.add(bl[bi]);
    bi++;
    } else {
    tl.add(cl[ci]);
    ci++;
    }
    } else {
    tl.add(bl[bi]);
    bi++;
    }

    } else if (ci != cl.length) {
    tl.add(cl[ci]);
    ci++;
    } else {
    break;
    }
    }
    return tl;
    }

Leave a Reply