整型数组a和b,编程实现:将a、b中不同的数字保存到一个新的数组中
如int[] a={10,20,30,40,50}
  int[] b={10,20,60}
得到:int[] c={30,40,50,60}

解决方案 »

  1.   

    1.开辟新数组,将b或a所有元素加入其中
    2.逐一比较a或b数组,是否与另一数组有重复元素,若无,加入新数组,若有,继续拿下一元素进行比较
    3.排序新数组
      

  2.   

    int i,j,x;
      x=0;
      for(i=0; i<sizeof(a); i++)
         for(j=0; j<sizeof(b); j++)
         {
             if(a[i]=b[j])
             {
                 c[x]=a[i];
                 x++;
             }
         }最简单最没效率的方法sizeof算数组大小 JAVA里没有
      

  3.   

    int[] a={10,20,30,40,50,60};
            int[] b={20,30,60};
            int[] result;
            int index=0;
            
            List list=new ArrayList();
            
            for(int i=0;i<a.length;i++)
                for(int j=0;j<b.length;j++){
                if(a[i]==b[j]){
                    list.add(index,a[i]);
                    index++;
                }
                }
            result=new int[list.size()];
            for(int i=0; i<list.size();i++){
                result[i]=Integer.parseInt(list.get(i).toString());
            }
      

  4.   

    1.开辟一个新的动态数组c.
    2.将a和b的第一个相比较,将小的那个放入c中,再取a和b中刚才比较小的那组的第2个.相同的继续在该组中取,一直去到最后2个数组中一个放如C结束.
      呵呵,在c的数据结果上学到的
      

  5.   

    先把a数组的数据全部加到一个List中,然后逐个判断b中的每个元素,如果在List中有,则remove掉,如果没有则加到List中,最后打印结果就可以了
    int[] a={10,20,30,40,50,60};
            int[] b={20,30,60};
            int[] result;
            int index=0;List tempList = new ArrayList();
    for (int i=0;i<a.length;i++){
       tempList.add(a[i]);
    }for (int j=0;j<b.length;j++){
       if (tempList.contain(b[j]))
           tempList.remove(b[j]);
       tempList.add(b[j]);
    }        result=new int[tempList.size()];
            for(int i=0; i<tempList.size();i++){
                result[i]=Integer.parseInt(tempList.get(i).toString());
            }
      

  6.   

    seanla(夜黑心死)的好象效率最高,
    不过还要加个排序
      

  7.   

    先感想大家的帮忙!
    seanla(夜黑心死)的看着挺好,但是我试了下,出现了异常
    if (tempList.contain(b[j])) 
           tempList.remove(b[j]);//在这里抛出了
                          java.lang.IndexOutOfBoundsException: Index: 10, Size: 5
       tempList.add(b[j]);
      

  8.   

    晕死哦,remove()方法接受的参数是对象的索引值,而你程序写的却是b[j],第一个重复的数值是10,所以10就被当作索引值传进去了,索引值最大是5,当然要报越界异常了..........
      

  9.   

    我觉得你可以写成 tempList.remove(tempList.indexof(b[j]);这样传进去的是索引,不过我记的List中好象不能直接放入primitive类型的数据吧,好象要先转成Object的.我建议不要用List,用Set,这样的话将数组a和b的值全加进去,而且不会加如重复的值(好象有一种Set还可以直接排序,那样的话连排序都省了),然后将这个Set里的值取出来放入一新数组即可.
      

  10.   

    多谢大家的帮助!我已经做出来了
    int[] a={10,20,30,40,50};
        int[] b={10,20,60};
        int[] result;
    ArrayList temp=new ArrayList();
    for(int i=0;i<a.length;i++){
    temp.add(a[i]);
    }
    for(int j=0;j<b.length;j++){
    Integer bb=new Integer(b[j]);
    if(temp.contains(bb)){
    temp.remove(bb);
    continue;    //关键
    }
    temp.add(bb);
    }
    result =new int[temp.size()];
    for(int m=0;m<temp.size();m++){
    result[m]=Integer.parseInt(temp.get(m).toString());
    System.out.println(result[m]);
    }
    结果为
    30
    40
    50
    60
    如果不加continue的话就是:
    30
    40
    50
    10
    20
    60
    先remove了,再循环发现没有又将10,20加进来了
      

  11.   

    既然用java了,还是用set比较好吧,省得判断是否相等了。
      

  12.   

    int[] a = { 10, 20, 30, 40, 50 };
            int[] b = { 10, 20, 60 };
            Set set = new TreeSet();
            int aLength = a.length;
            int bLength = b.length;
            for (int i = 0; i < aLength; i++) {
                set.add(new Integer(a[i]));
            }
            for (int j = 0; j < bLength; j++) {
                if (set.contains(new Integer(b[j]))) {
                    set.remove(new Integer(b[j]));
                } else {
                    set.add(new Integer(b[j]));
                }
            }
            Integer[] result = (Integer[]) set.toArray(new Integer[0]);
            System.out.println(result.length);
      

  13.   

    public class test {
    public static void main(String[] arg) {
    int a[]={10,20,30,40,50};
    int b[]={10,20,60};
    int total=0, c[]=new int[a.length+b.length];
    for(int i=0; i<a.length; i++)
    if(isDif(a[i],b)) c[total++]=a[i];
    for(int i=0; i<b.length; i++)
    if(isDif(b[i],a)) c[total++]=b[i];
    for(int i=0; i<total; i++) System.out.print(c[i]+"    ");
    }

    static boolean isDif(int x, int[] y) {
    boolean ok=true;
    for(int i=0; i<y.length; i++)
    if(x==y[i]) ok=false;
    return ok;
    }
    }
      

  14.   

    int[] a = {10, 20, 50, 60, 70};
     int[] b = {10, 30, 50};
     ArrayList<Integer> lists = new ArrayList<Integer>();
     for(int i : a) {
     for(int j : b) {
     if(i == j){
     break;
     }
     lists.add(i);
     break;
     }
     }
      

  15.   

    给你一个不用collection写的:
         int[] a = {10,20,30,40,50,70,80,90};
         int[] b = {10,20,60,40,30};
         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);
             
         int count = 0;
         for (int i = 0; i < c.length; i++) {
         for (int j = i + 1; j < c.length; j++) {
         if (c[i] == c[j]) {    
         c[i] = c[j] = 0;
         count++;
         break;
         }
         }
         }                int[] d = new int[c.length - (count+1)];
         count= 0;
         for (int i = 0; i < c.length; i++) {
         if (c[i] != 0) d[count++] = c[i];   
         }
        
         for (int i = 0; i < d.length; i++) {    
         System.out.println(d[i]);    
         }
      

  16.   

    public class CollectionTest {    public static void main(String[] args) {
            int[] a = {10,20,30,40,50};
            int[] b = {10,20,60};        Set setA = new TreeSet();
            Set setB = new TreeSet();        for (int i = 0; i< a.length; i++) {
                setA.add(new Integer(a[i]));
            }
            for (int i = 0; i< b.length; i++) {
                setB.add(new Integer(b[i]));
            }        Set a_substract_b = new TreeSet(setA);
            a_substract_b.removeAll(setB);        Set b_substract_a = new TreeSet(setB);
            b_substract_a.removeAll(setA);        Set setC = new TreeSet(a_substract_b);
            setC.addAll(b_substract_a);        System.out.println(setC.toString());    }
    }
      

  17.   

    int[] a={4,2,6,78,2,5};
        int[] b={3,5,6,3,7};
        ArrayList c=new ArrayList();
        
        Arrays.sort(a);
    Arrays.sort(b);

    int index=0;
    for(int i=0;i<a.length;i++)
    {
    if((index=Arrays.binarySearch(b,a[i]))<0)
        
    c.add(a[i]);
    }

    for(int i=0;i<b.length;i++)
    {
    if((index=Arrays.binarySearch(a,b[i]))<0)
        c.add(b[i]);
    }


    Object[] d=c.toArray();
    for(int i=0;i<d.length;i++)
    {
    System.out.println(d[i]);
    }
      

  18.   

    Java的集合框架多好啊? 直接就搞定了 JDK 1.4.2
      

  19.   

    public void display() {
    int[] a = { 10, 20, 30, 40, 50, 60 };
    int[] b = { 20, 30, 60 };
    int[] result; List tempList = new ArrayList();
    for (int i = 0; i < a.length; i++) {
    tempList.add(new Integer(a[i]));
    } for (int j = 0; j < b.length; j++) {
    if (tempList.contains(b[j]))
    tempList.remove(new Integer(b[j]));
    else
    tempList.add(new Integer(b[j]));
    } result = new int[tempList.size()];
    for (int i = 0; i < tempList.size(); i++) {
    result[i] = Integer.parseInt(tempList.get(i).toString());
    System.out.println(result[i]);
    } }