Java 数组合并排序

1、利用System.arraycopy进行合并

import java.util.*;
public class ArrayMerge{
    public static void main(String[] args){
        int[] a = {1,2,3,5,6,7};
        int[] b = {5,7,,84,9,2};
        int[] c = merge(a, b);
        System.out.println(Arrays.toString(c));
    }

    public static int[] merge(int[] a, int[] b){
        int[] c = new int[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        Arrays.sort(c); // 对数组进行排序
        return c;
    }
}

你可能感兴趣的:(Java)