如 数组1:[0,1,2,3,4]
数组2:[0,1,2]
那么怎么找出不同的元素3,4呢??
双循环好像去不出来???
求代码

解决方案 »

  1.   


    import java.util.HashMap;
    import java.util.Map;
    public class test2 {

    public static void main(String[] args) {
    int[] num2 = {0, 1, 2, 3, 4, 5};
    int[] num1 = {0, 1, 2, 7, 8, 9};
    Map<Integer, Integer> map1 = new HashMap<Integer, Integer>();
    Map<Integer, Integer> map2 = new HashMap<Integer, Integer>();
    int big = num2.length > num1.length ? num2.length : num1.length;
    int small = num2.length <= num1.length ? num2.length : num1.length;
    int[] tmp1 = num2.length > num1.length ? num2 : num1;
    int[] tmp2 = num2.length <= num1.length ? num2 : num1;
    for (int i = 0; i < small; i++) {
    map1.put(tmp2[i], 0);
    }
    for (int i = 0; i < big; i++) {
    map2.put(tmp1[i], 0);
    if (!map1.containsKey(tmp1[i])) {
    System.out.println(tmp1[i]);
    }
    }
    for (int i = 0; i < small; i++) {
    map1.put(tmp2[i], 0);
    if (!map2.containsKey(tmp2[i])) {
    System.out.println(tmp2[i]);
    }
    }
    }
    }
      

  2.   


    public class test{
     public static void main(String arg[]){
    int a[] = {1,2,3,4,5};
    int b[] = {1,2,5};
    for(int i=0;i<a.length;i++)
    {
    if(i>b.length-1){

    System.out.print(a[i]);

    }else if(a[i]!=b[i])
    {
    System.out.print(a[i]);
    }

    }
    }
    }
      

  3.   

    表a,表b
    循环遍历表a,从b里面删除a[i],判断是否删除成功,若成功,则从a里面删除a[i],否则,不操作。
    最后a表和b表中剩下的就是不同的。