Java Array Merge mergeArrays(String[] data1, String[] data2)

Here you can find the source of mergeArrays(String[] data1, String[] data2)

Description

Merge 2 string's arrays data1 and data2 to one array of string 'data1 + data2'.

License

Apache License

Parameter

Parameter Description
data1 - first array of string
data2 - second array of string

Return

- merged array or null if both arrays is null

Declaration

public static synchronized String[] mergeArrays(String[] data1, String[] data2) 

Method Source Code

//package com.java2s;
/*/*from   w w  w.  j  a  v a  2s.  c  o m*/
Copyright 2005-2006 The Apache Software Foundation or its licensors, as applicable
    
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.
*/

public class Main {
    /**
     * Merge 2 string's arrays data1 and data2 to one array of string 'data1 +
     * data2'.
     * 
     * @param data1 - first array of string
     * @param data2 - second array of string
     * @return - merged array or null if both arrays is null
     */
    public static synchronized String[] mergeArrays(String[] data1, String[] data2) {
        if (data1 == null && data2 == null) {
            return null;
        }
        if (data1 == null) {
            return data2;
        }
        if (data2 == null) {
            return data1;
        }
        int cnt = 0;
        String[] retVal = new String[data1.length + data2.length];
        for (int i = 0; i < data1.length; i++) {
            retVal[cnt++] = data1[i];
        }
        for (int i = 0; i < data2.length; i++) {
            retVal[cnt++] = data2[i];
        }
        return retVal;
    }
}

Related

  1. mergeArrays(Object[] arr1, Object[] arr2, Object[] destinationArray)
  2. mergeArrays(Object[] first, Object[] second)
  3. mergeArrays(String[] a, String[] b)
  4. mergeArrays(String[] a1, String[] a2)
  5. mergeArrays(String[] array, String[] newEntries)
  6. mergeArrays(String[] inputArray1, String[] inputArray2)
  7. mergeArrays(T[] items, T[]... added)
  8. mergeArrays(T[] lhs, T[] rhs)
  9. MergeArrays(T[]... arrs)