1. 程式人生 > 其它 >兩個陣列去掉重複資料

兩個陣列去掉重複資料

public static void main(String[] args) {
    String[] stra = {"_zxl","2","3"};
    String[] strb = {"_zxl","2","3"};
    System.out.println(Arrays.toString(remDuplicate(stra , strb)));
}

public static Object[] remDuplicate(String[] stra,String [] strb){
        String [] resStrArr = new String[stra.length+strb.length];
        //淺複製A的資料
        String[] temArr = Arrays.copyOf(stra, stra.length + strb.length);
//        陣列的擴容
//        src:源陣列
//        srcPos:在源陣列中,開始複製的位置
//        dest:目標陣列
//        destPos:在目標陣列中,開始賦值的位置
//        length:要複製的長度
        //arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);
        System.arraycopy(strb, 0, temArr, stra.length, strb.length);
        System.out.println(Arrays.toString(temArr));
        StringBuffer sb = new StringBuffer();
        String patter = "_zxl";
        int i = 0;
        for(String str :temArr){
            if(sb.indexOf(str+patter)==-1){
                sb.append(str+"_zxl,");
                resStrArr[i]=str;
                i++;
            }
        }
        String [] resArr = new String[i];
        System.arraycopy(resStrArr, 0, resArr, 0, i);
        return resArr;
}