Java Array Merge mergeArrays(String[] a, String[] b)

Here you can find the source of mergeArrays(String[] a, String[] b)

Description

Create the union of two string arrays.

License

LGPL

Parameter

Parameter Description
a A string array
b A string array

Return

The union of a and b

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String[] mergeArrays(String[] a, String[] b) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    /**//from ww  w .j av a  2s  . c  o  m
     * Create the union of two string arrays.
     * Duplicate entries are removed.
     *
     * @param a A string array
     * @param b A string array
     * @return The union of <code>a</code> and <code>b</code>
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static String[] mergeArrays(String[] a, String[] b) {
        if (a == null) {
            return b;
        }
        if (b == null) {
            return a;
        }

        // found all names in a which are not in b
        Vector v = new Vector();
        for (int i = 0; i < a.length; i++) {
            int j = 0;
            boolean found = false;
            do {
                if (a[i].equalsIgnoreCase(b[j])) {
                    found = true;
                } else {
                    j++;
                }
            } while (!found && j < b.length);

            if (!found) {
                v.add(a[i]);
            }
        }

        String[] c = new String[b.length + v.size()];

        Enumeration aEnum = v.elements();
        int i = 0;
        while (aEnum.hasMoreElements()) {
            c[i++] = (String) aEnum.nextElement();
        }

        for (int j = 0; j < b.length; j++) {
            c[i++] = b[j];
        }

        return c;
    }
}

Related

  1. mergeArrays(final Object[] a1, final Object[] a2, final Object[] merge)
  2. mergeArrays(int[] theArrayA, int[] theArrayB)
  3. mergeArrays(long[] alldistances, long[] currentUserDistances)
  4. mergeArrays(Object[] arr1, Object[] arr2, Object[] destinationArray)
  5. mergeArrays(Object[] first, Object[] second)
  6. mergeArrays(String[] a1, String[] a2)
  7. mergeArrays(String[] array, String[] newEntries)
  8. mergeArrays(String[] data1, String[] data2)
  9. mergeArrays(String[] inputArray1, String[] inputArray2)