Java Array Merge merge(String[] strArr1, String[] strArr2)

Here you can find the source of merge(String[] strArr1, String[] strArr2)

Description

Merge the two given arrays of strings

License

Apache License

Parameter

Parameter Description
strArr1 Array 1
strArr2 Array 2

Return

String[] Array containing all the strings of strArr1 and strArr2

Declaration

public static String[] merge(String[] strArr1, String[] strArr2) 

Method Source Code


//package com.java2s;
/*/*from   ww  w .  ja  v a 2  s .co  m*/
 * Copyright 2012-2014 Kay Stenschke
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Merge the two given arrays of strings
     *
     * @param   strArr1      Array 1
     * @param   strArr2      Array 2
     * @return   String[]   Array containing all the strings of strArr1 and strArr2
     */
    public static String[] merge(String[] strArr1, String[] strArr2) {
        if (strArr1 == null && strArr2 != null)
            return strArr2;
        if (strArr2 == null && strArr1 != null)
            return strArr1;
        if (strArr1 == null)
            return null;

        List<String> list = new ArrayList<String>(Arrays.asList(strArr1));
        list.addAll(Arrays.asList(strArr2));

        Object[] strObjects = list.toArray();

        String[] merged = {};

        int count = 0;
        for (Object strObject : strObjects) {
            merged[count] = strObject.toString();
            count++;
        }

        return merged;
    }
}

Related

  1. merge(long[] theArray, long[] workSpace, int lowPtr, int highPtr, int upperBound)
  2. merge(Object[][] array1, Object[][] array2)
  3. merge(String array[], String delimiter)
  4. merge(String[] array)
  5. merge(String[] array1, String[] array2)
  6. merge(String[] strArray)
  7. merge(String[]... arrays)
  8. merge(T[] array, T[] temp, int left, int middle, int right)
  9. merge(T[] array1, T[] array2)