Java Array Merge mergeAndOrderArray(int[] arrA, int[] arrB)

Here you can find the source of mergeAndOrderArray(int[] arrA, int[] arrB)

Description

merge And Order Array

License

Open Source License

Parameter

Parameter Description
arrA a parameter
arrB a parameter

Declaration

public static int[] mergeAndOrderArray(int[] arrA, int[] arrB) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w w  w .  j  av  a 2 s . c  o m
     * 
     * @param arrA
     * @param arrB
     * @return 
     */
    public static int[] mergeAndOrderArray(int[] arrA, int[] arrB) {
        int[] arr = new int[arrA.length + arrB.length];

        int a = 0;
        int b = 0;

        for (int i = 0; i < arr.length; i++) {
            if (a == arrA.length) {
                arr[i] = arrB[b];
                b++;
            } else if (b == arrB.length) {
                arr[i] = arrA[a];
                a++;
            } else if (arrA[a] < arrB[b]) {
                arr[i] = arrA[a];
                a++;
            } else {
                arr[i] = arrB[b];
                b++;
            }
        }

        return arr;
    }
}

Related

  1. merge(T[] first, T[] second)
  2. merge(T[] left, T... right)
  3. merge(T[]... arrays)
  4. merge(T[]... many)
  5. merge2TablesWithoutDup(String[] t1, String[] t2)
  6. mergeArgs(String[] args)
  7. mergeArray(byte[] arr1, byte[] arr2)
  8. mergeArray(final Object[] dest, final Object[]... arrays)
  9. mergeArray(String[] a, String[] b)