Java Array Merge mergeArrays(String[] inputArray1, String[] inputArray2)

Here you can find the source of mergeArrays(String[] inputArray1, String[] inputArray2)

Description

This method merges 2 input string arrays and returns the merged array as output

License

Open Source License

Parameter

Parameter Description
inputArray1 Input Array 1 that needs to be merged
inputArray2 Input Array 2 that needs to be merged

Return

Returns merged Array that contains all elements of inputArray1 followed by elements of inputArray2

Example: The following are the input array

inputArray1 contains elements ("a","b","c")

inputArray2 contains element ("b","e")

then the output array contains elements

("a","b","c","b","e")

Declaration

public static String[] mergeArrays(String[] inputArray1, String[] inputArray2) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  ww. j a  v  a2  s  .  c  o  m
    * This method merges 2 input string arrays and returns the merged array as output
    *
    * @param inputArray1 Input Array 1 that needs to be merged
    * @param inputArray2 Input Array 2 that needs to be merged
    * @return Returns merged Array that contains all elements of inputArray1 followed by elements of inputArray2
    *         <p/>
    *         <b>Example:</b> The following are the input array
    *         <p/>
    *         inputArray1 contains elements ("a","b","c")
    *         <p/>
    *         inputArray2 contains element ("b","e")
    *         <p/>
    *         then the output array contains elements
    *         <p/>
    *         ("a","b","c","b","e")
    */
    public static String[] mergeArrays(String[] inputArray1, String[] inputArray2) {
        String[] outputArray = new String[inputArray1.length + inputArray2.length];
        /**
        * Copy first array to outputArray
        */
        for (int i = 0; i < inputArray1.length; i++) {
            outputArray[i] = inputArray1[i];
        }

        /**
        * Copy second array to outputArray
        */
        int outputArrayLength = inputArray1.length;
        for (int i = 0; i < inputArray2.length; i++) {
            outputArray[i + outputArrayLength] = inputArray2[i];
        }

        return outputArray;
    }
}

Related

  1. mergeArrays(Object[] first, Object[] second)
  2. mergeArrays(String[] a, String[] b)
  3. mergeArrays(String[] a1, String[] a2)
  4. mergeArrays(String[] array, String[] newEntries)
  5. mergeArrays(String[] data1, String[] data2)
  6. mergeArrays(T[] items, T[]... added)
  7. mergeArrays(T[] lhs, T[] rhs)
  8. MergeArrays(T[]... arrs)
  9. mergeArrayToString(final String[] array)