Java错误中的交集和联合

时间:2012-04-29 01:50:13

标签: java arrays union intersection

我是Java新手,我被要求做交叉和联合。我有这个代码,我不知道问题是什么。

我的代码无需扫描仪输入=新的扫描仪(System.in);

import java.util.*;    
import java.util.ArrayList;

public class Arrays{
    Scanner input=new Scanner(System.in);

    private static int[] array_one = new int[5];
    for(i=0;i<5;i++){
        array_one[i]=input.nextInt();
    }
    private static int[] array_two = new int[5];
    for(i=0;i<5;i++){
        array_two[i]=input.nextInt();
    }

    public static void main(String main[]){
        System.out.println("First Array");
        System.out.println(Arrays.toString(array_one));
        System.out.println("Second Array");
        System.out.println(Arrays.toString(array_two));

        int[] union = getUnion(array_one, array_two);

        System.out.println("--- Union of sets ---");

        for(int i=0; i<union.length; i++){    
            System.out.print(union[i] + " ");
        }    
        System.out.println("");    
        Integer[] intersection = getIntersection(array_one, array_two);    
        System.out.println("--- Intersection of sets ---");    
        for(int i=0; i<intersection.length; i++) {    
            System.out.print(intersection[i] + " ");    
        }    
        System.out.println("");    
        System.out.println("\nIs The Two Array Equal?");
        boolean check = Arrays.equals(array_one, array_two);
        if(check == false)
        System.out.println("= Arrays Are Not Equal");
        else
        System.out.println("= Arrays Are Equal");
    }        

    private static int[] getUnion(int[] a_one, int[] a_two){

        int i=0, j=0;    
        int value = -1;    
        int MAX_ELEM = 0;    
        int[] a_return = new int[a_one.length + a_two.length -1];

        try{    
            while(i<a_one.length || j<a_two.length){    
                if(a_one[i] < a_two[j]){
                    value = a_one[i];    
                    i++;    
                }else{    
                  value = a_two[j];    
                    j++;    
                }    
                if(!found(a_return, value, 0, MAX_ELEM)){    
                    a_return[MAX_ELEM++] = value;    
                }    
            }    
        } catch (IndexOutOfBoundsException ex){    
            if(i == a_one.length){    
                for(int k=j; k<a_two.length; k++){    
                    if(!found(a_return, a_two[k], 0, MAX_ELEM)){    
                        a_return[MAX_ELEM++] = a_two[k];    
                    }    
                }    
            } else {    
                for(int l=i; l<a_one.length; l++){

                    if(!found(a_return, a_one[l], 0, MAX_ELEM)){    
                        a_return[MAX_ELEM++] = a_one[l];    
                    }    
              }    
            }    
        }    
        return a_return;    
    }

    private static Integer[] getIntersection(int[] a_one, int[] a_two){    
        ArrayList<Integer> a_list = new ArrayList<Integer>();    
        int i=0, j=0;    
        while(i < a_one.length && j< a_two.length){    
            if(a_one[i] == a_two[j]){
                    a_list.add(a_one[i]);    
                i++;    
                j++;    
            }else if(a_one[i] < a_two[j]){    
                i++;    
            }else if(a_one[i] > a_two[j]){    
                j++;    
            }    
        }    
        Integer[] a_return = new Integer[a_list.size()];    
        a_list.toArray(a_return);    
        return a_return;    
    }

    /* -------- linear search --------- */    
    private static boolean found1(int[] array, int value,int start, int end){    
       for(int i=0; i<array.length; i++){    
            if( array[i] == value){    
                return true;    
            }    
        }    
        return false;    
    }

    /* ---------- binary search ------------- */
    private static boolean found(int[] array, int value, int start, int end){    
        if(end < 0 || start < 0){    
            return false;    
        }    
        if(array[start] == value || array[end] == value){    
            return true;
        }
        if(end-start == 1){    
            if(array[end] == value){    
                return true;    
            }    
            if(array[start] == value){    
                return true;    
            }    
        }else{    
            int mid = (start + end)/2;    
            if(array[mid] == value){    
                return true;    
            }else if(array[mid] < value){    
                return found1(array, value, mid+1, end);    
            }else if(array[mid] > value){    
               return found1(array, value, start, mid-1);    
            }    
        }    
       return false;    
    }    
}

2 个答案:

答案 0 :(得分:2)

如果您可以使用Java Collections,我会在我的一个作业中使用它。

public static Set<Integer> union(Set<Integer> a, Set<Integer> b) {
    Set<Integer> union = new HashSet<>();
    union.addAll(a);
    union.addAll(b);
    return union;
}
祝你好运!

答案 1 :(得分:0)

你应该有这个代码

private static int[] array_one = new int[5];
for(i=0;i<5;i++){
    array_one[i]=input.nextInt();
}
private static int[] array_two = new int[5];
for(i=0;i<5;i++){
    array_two[i]=input.nextInt();
}

private static int[] array_one = new int[5];
for(i=0;i<5;i++){
    array_one[i]=input.nextInt();
}
private static int[] array_two = new int[5];
for(i=0;i<5;i++){
    array_two[i]=input.nextInt();
}

在main()或任何方法或static block内。

This链接可能会有所帮助。