方法1:根据有序进行插入
//合并两个有序的数组后还为有序数组
import java.util.ArrayList;
import java.util.List;
public class OrderArray {
public static void concat(int [] arr1, int [] arr2){
List<Integer> list = new ArrayList<>();
int mark = 0;
int j = 0;
for(int i = 0; i<arr1.length; i++){
lable1:
for(j = mark; j<arr2.length;j++){
if(arr1[i]<arr2[j]){
list.add(arr1[i]);
break lable1;
}else if(arr1[i]>arr2[j]){
list.add(arr2[j]);
mark = j+1;
}else if(arr1[i]==arr2[j]){
list.add(arr1[i]);
list.add(arr2[j]);
mark = j+1;
break lable1;
}
}
if(j == arr2.length){
list.add(arr1[i]);
}
}
System.out.println(list);
}
public static void main (String [] args){
int [] arr1 = {2,7,9,10,12};
int [] arr2 = {1,3,5,7,8};
concat(arr1,arr2);
}
}
结果:
[1, 2, 3, 5, 7, 7, 8, 9, 10, 12]
方法2:
将两个数组直接合并,再进行排序