找到两个单维数组的并集,交集和差异

时间:2014-09-28 01:25:51

标签: java arrays

我正在尝试创建一个程序,该程序提供由用户输入创建的两个数组的并集,交集和差异。因此,如果用户希望第一个数组的大小为4,元素为[1 2 3 4],第二个数组的大小为5,元素为[3 4 5 6 7],那么输出应该返回;集合A和集合B的并集是:1 2 3 4 5 6 7                               集合A和集合B的交集是:3 4                               集合A和集合B的差异是:1 2 现在已经获得了交集,但差异并不起作用。差异打印为第一个数组的所有元素。我不知道从哪里开始联盟。这是我的代码:

package rhc91310a13sets;

import java.util.*;

public class rhc91310a13sets {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] setA = null;
        int[] setB = null;

        System.out.print("Enter size for set A (must be less than or equal to 20): ");
        setA = new int[sc.nextInt()];

        System.out.print("Enter elements for set A between 1-20: ");
        for (int i = 0; i < setA.length; i++) {
            setA[i] = sc.nextInt();
        }

        System.out.print('\n');

        System.out.print("Enter size for set B (must be less than or equal to 20): ");
        setB = new int[sc.nextInt()];

        System.out.print("Enter elements for set B between 1-20: ");
        for (int i = 0; i < setB.length; i++) {
            setB[i] = sc.nextInt();
        }

        System.out.print('\n');

        System.out.print("The union of sets A and B are: ");
        for(int i = 0; i < setA.length; i++) {
            for(int j = 0; j < setB.length; j++) {
                }
            }       

        System.out.print('\n');

        System.out.print("The intersection of sets A and B are: ");
        for (int i = 0; i < setA.length; i++) {
            for (int j = 0; j < setB.length; j++) {
                if (setA[i] == setB[j]) {
                    System.out.print(setA[i] + " ");
                }
            }
        }

        System.out.print('\n');

        System.out.print("The difference of sets A and B are: ");
        for (int i = 0; i < setA.length; i++) {
            for (int j = 0; j < setB.length; j++) {
                if (!(setA[i] == setB[j])) 
                    System.out.print(setA[i] + " ");        
            }
        }
    }
  }

1 个答案:

答案 0 :(得分:1)

您的意外结果

The difference of sets A and B are: 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4

你的逻辑有一些问题。你通过从setA中取一个元素来检查setAset B之间的区别,并检查它是否与setB的元素不同,但问题就在于此。例如你从setA中取1并检查它是否与3 ,4, 5, 6 ,7不同。所以它与所有5个元素不同,因此1将打印5次。当你从setA获取3时出现最严重的问题检查setB.3与所有元素不同,期望setB.so 3中的第一个元素将打印4 times。但是请等待这个想法。如果setA打印less than 5 times的元素,意味着它不是不同的元素。你需要做的是检查它是不同的所有5个元素而不是单个,我把计数变量和检查是它与setB的5倍不同。但我认为简单的方法是使用HashSet

int[] setA = {1,2,3,4};
int[] setB = {3 ,4, 5, 6 ,7};
int count=0;
System.out.print("The difference of sets A and B are: \n");
for (int i = 0; i < setA.length; i++) {
    count=0;
    for (int j = 0; j < setB.length ; j++) {
        if ((setA[i] != setB[j])){ 
            count++;

        } 
        if(count==setB.length){
            System.out.println(setA[i]);
        }
    }
}

输出&GT;&GT;

The difference of sets A and B are: 
1
2

<强> *** ***更新

如果你想把这个2的不同元素放在一个数组中,最好的方法是arraylist。你可以这样做

int[] setA = {1, 2, 3, 4};
int[] setB = {3, 4, 5, 6, 7};
int count = 0;
ArrayList<Integer> arl = new ArrayList<Integer>();

System.out.print("The difference of sets A and B are: \n");
for (int i = 0; i < setA.length; i++) {
    count = 0;
    for (int j = 0; j < setB.length; j++) {
        if ((setA[i] != setB[j])) {
            count++;

        }
        if (count == setB.length) {
           // System.out.println(setA[i]);
            arl.add(i);
        }
    }
}
System.out.println(arl);

输出&GT;&GT;

The difference of sets A and B are: 
1
2
[0, 1]

找到联合值。你可以使用HashSet。

ArrayList list = new ArrayList();
for(int i=0;i<setA.length;i++){
    list.add(setA[i]);
}
for(int i=0;i<setB.length;i++){
    list.add(setB[i]);
}
HashSet h = new HashSet();
h.addAll(list);
list.clear();
list .addAll(h);
System.out.println(list);