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;
}
}
两个从小到大的数组组合成一个从小到大的数组
最新推荐文章于 2024-06-01 16:56:34 发布