两个从小到大的数组组合成一个从小到大的数组

来源:互联网 发布:阿里云备案订单在哪里 编辑:程序博客网 时间:2024/05/01 00:51
public class Main {public static void main(String[] args) { int[] a={1,3,5,6}; int[] b={2,4,7}; int[] c = new int[7];  c=Func(a, b);//排列  for (int i = 0; i < c.length; i++) {System.out.println(c[i]); }}public static int[] Func(int[] m, int[] n){    if (m == null || n == null)    {    return null;    }     int[] result = new int[m.length + n.length];    int mIndex = 0;    int nIndex = 0;    for (int index = 0; index < result.length; index++)    {    //大于m数组长度mIndex=4        if (mIndex >= m.length)        {            result[index] = n[nIndex];            continue;        }        //大于n数组长度nIndex=3        if (nIndex >= n.length)        {            result[index] = m[mIndex];            continue;        }                    //排序关键        if (m[mIndex] < n[nIndex])        {                   result[index] = m[mIndex];           mIndex++;                   }else{                    result[index] = n[nIndex];            nIndex++;        }    }      return result;}}

0 0