文章目录
- 1.利用arraycope的方法
- 2.将两数组合并 ,在排序
1.利用arraycope的方法
public class MergeArr {
public static void main(String[] args) {
int[] arr1 = {1,2,3,4,5,6};
int[] arr2 = {7,8,9};
int[] arr3 = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, arr3, 0, arr1.length);
System.arraycopy(arr2, 0, arr3, arr1.length, arr2.length);
System.out.println(Arrays.toString(arr3));
}
}
2.将两数组合并 ,在排序
public static void main(String[] args) {
int[] arr1 = {1,2,3};
int[] arr2 = {1,4,5};
int[] arr3 = mergeArr(arr1,arr2);
compareArr(arr3);
for (int i =0;i<arr3.length;i++){
System.out.println(arr3[i]+" ");
}
}
public static int[] mergeArr(int[] arr1,int[] arr2 ){
int[] arr3 = new int[arr1.length+arr2.length];
for(int i =0;i<arr1.length;i++){
arr3[i] = arr1[i];
}
for(int i =0;i<arr2.length;i++){
arr3[arr1.length+i] = arr2[i];
}
return arr3;
}
public static void compareArr(int[] arr ){
for(int i =0;i<arr.length-1;i++){
for(int j = 0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}