merge Two String Arrays - Java Collection Framework

Java examples for Collection Framework:Array Merge

Description

merge Two String Arrays

Demo Code


//package com.book2s;

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

    public static String[] mergeTwoStringArrays(String[] pa, String[] pb) {

        if (pa == null)
            return pb;

        if (pb == null)
            return pa;

        String[] arr = new String[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