merge Two Object Arrays - Java Collection Framework

Java examples for Collection Framework:Array Merge

Description

merge Two Object Arrays

Demo Code


//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        Object[] pa = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        Object[] pb = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(java.util.Arrays.toString(mergeTwoObjectArrays(
                pa, pb)));/*ww  w  .  j a  va2  s  .  c  o m*/
    }

    public static Object[] mergeTwoObjectArrays(Object[] pa, Object[] pb) {

        if (pa == null)
            return pb;

        if (pb == null)
            return pa;

        Object[] arr = new Object[pa.length + pb.length];
        for (int x = 0; x < pa.length; x++) {
            arr[x] = pa[x];
        }
        for (int x = 0; x < pb.length; x++) {
            arr[x + pa.length] = pb[x];
        }
        return arr;
    }
}

Related Tutorials