-4

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

我的代码在没有扫描仪输入的情况下工作 = new Scanner(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;    
    }    
}
4

2 回答 2

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;
}

祝你好运!

于 2012-04-29T03:31:30.790 回答
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() 或任何方法或静态块内。

链接可能会有所帮助。

于 2012-04-29T02:25:54.203 回答