How to Merge Two Arrays in Java


In this example, we will learn a few methods to join two arrays in Java.

Source Code

Method 1:

package com.beginner.examples;

public class JoinArrayExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] nums = { "1", "2", "3", "5", "6" };
        String[] nums2 = { "7", "8", "9" };

        ArrayUtils.printArray(ArrayUtils.addAll(nums, nums2));
    }

    static class ArrayUtils {
        static void printArray(Object[] s) {
            System.out.print("[");
            for (int i = 0; i < s.length; i++) {
                if (i == s.length - 1)
                    System.out.print(s[i] + "]");
                else {
                    System.out.print(s[i] + ",");
                }

            }
        }
        public static Object[] addAll(Object[] s1, Object[] s2) {
            Object[] s3 = new String[s1.length + s2.length];
            for (int i = 0; i < s3.length; i++) {
                if (i < s1.length)
                    s3[i] = s1[i];
                else {
                    s3[i] = s2[i - s1.length];
                }

            }
            return s3;

        }
    }
}

Output:

[1,2,3,5,6,7,8,9]

Method 2:

package com.beginner.examples;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JoinArrayExample2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] s1 = new String[] { "a", "b", "c" };
        String[] s2 = new String[] { "d", "e", "f" };
        String[] s3 = toStrings(getTwoArray(s1, s2));
        System.out.println("s1: " + Arrays.toString(s1));
        System.out.println("s2: " + Arrays.toString(s2));
        System.out.println("s3: " + Arrays.toString(s3));

    }
    public static Object[] getTwoArray(Object[] a, Object[] b) {

        List aL = Arrays.asList(a);
        List bL = Arrays.asList(b);
        List resultList = new ArrayList();
        resultList.addAll(aL);
        resultList.addAll(bL);
        Object[] result = resultList.toArray();
        return result;

    }
    public static String[] toStrings(Object[] objs) {
        String[] strings = new String[objs.length];
        for (int i = 0; i < objs.length; i++) {
            strings[i] = objs[i].toString();
        }
        return strings;

    }
}

Output:

s1: [a, b, c]
s2: [d, e, f]
s3: [a, b, c, d, e, f]

Method 3:

package com.beginner.examples;

import java.util.Arrays;

public class JoinArrayExample3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] a = { "0", "1", "2", "3" };
        String[] b = { "4", "5", "6", "7", "8" };
        String[] c = new String[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        System.out.println("a: " + Arrays.toString(a));
        System.out.println("b: " + Arrays.toString(b));
        System.out.println("c: " + Arrays.toString(c));
    }

}

Output:

a: [0, 1, 2, 3]
b: [4, 5, 6, 7, 8]
c: [0, 1, 2, 3, 4, 5, 6, 7, 8]

Tips

The simplest way is add the Apache Commons Lang library, and use ArrayUtils. addAll to join arrays. This method supports both primitive and object type arrays.

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments