Java Array Merge mergeStrings(String[] x, String[] y)

Here you can find the source of mergeStrings(String[] x, String[] y)

Description

merge Strings

License

Open Source License

Declaration

public static String[] mergeStrings(String[] x, String[] y) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static String[] mergeStrings(String[] x, String[] y) {
        if (x == null)
            return y;
        if (y == null)
            return x;

        List<String> mergedList = new ArrayList<String>();
        int xp = 0, yp = 0;
        while (xp < x.length && yp < y.length) {
            if (x[xp].compareTo(y[yp]) < 0) {
                mergedList.add(x[xp++]);
            } else if (x[xp].compareTo(y[yp]) > 0) {
                mergedList.add(y[yp++]);
            } else {
                mergedList.add(x[xp]);// w w  w .java 2s . c om
                xp++;
                yp++;
            }
        }
        while (xp < x.length) {
            mergedList.add(x[xp++]);
        }
        while (yp < y.length) {
            mergedList.add(y[yp++]);
        }
        return mergedList.toArray(new String[0]);
    }
}

Related

  1. mergeStringArrayFromIndex(String[] arrayString, int i)
  2. mergeStringArrays(String array1[], String array2[])
  3. mergeStringArrays(String array1[], String array2[], String array3[])
  4. mergeStringArrays(String[] array1, String[] array2)
  5. mergeStringArrays(String[][] arrayOfArrays)
  6. mergeToArray(T... args)