Java两个数组合并,去除重复元素

合并两个数组并且排序

 static int []a = {1,3,8,20};

    static int []b = {2,3,3,11,12,15,26,27};
    //定义下标  a=i b=j c=szie
    int i = 0;

    public int[] test(int []a,int []b) {
        Map map = new TreeMap<>();
        for (int i = 0; i < a.length; i++) {
            map.put(a[i],a[i]);
        }
        for (int i = 0; i < b.length; i++) {
            map.put(b[i],b[i]);
        }
        Collection values = map.values();
        Iterator iterator = values.iterator();
        int c [] = new int[values.size()];
        while (iterator.hasNext()){
            c[i++] = iterator.next();
        }
        return c;
    }
    public static void main(String[] args) {
        Test main = new Test();
        int []ia = main.test(a,b);
        System.out.println(Arrays.toString(ia));
    }

你可能感兴趣的:(Array,java)