【问题标题】:Finding union, intersection and difference of two single dimension arrays查找两个一维数组的并集、交集和差集
【发布时间】:2014-09-28 01:25:50
【问题描述】:

我正在尝试创建一个程序,该程序给出由用户输入创建的两个数组的并集、交集和差集。因此,如果用户希望第一个数组的大小为 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] + " ");        
            }
        }
    }
  }

【问题讨论】:

  • 为什么不使用 HashSet?那么所有这些操作都只是一个方法调用。
  • 问题是,比如说setA[0] == setB[1]setA[0] == setB[0] 可能是假的,这意味着它会被打印出来。您必须检查每个元素,只有在没有冲突的情况下:打印它。
  • 这是作业的味道

标签: java arrays


【解决方案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 .but 等等这给出了想法。如果 setA 的一个元素打印 less than 5 times 这意味着它不是不同的元素。您需要做的是检查所有 5 个元素是否不同,而不是单个元素,我放入 count 变量并检查它是否与 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]);
        }
    }
}

输出>>

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

输出>>

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

【讨论】:

  • 感谢您的帮助。我怀疑联合我需要创建第三个数组。
  • 你想把不同的元素放到一个数组中吗?
  • 这个想法是打印出第一个数组和第二个数组中的所有元素,而不打印出任何重复项。类似于 int[] setC = new int[setA.length + setB.length] 的东西会让我接近还是我离基地很远?再次感谢您的帮助。
  • @Hermes “不打印任何重复项”所以这不会给出重复项。你不想只得到 1 和 2 吗?
  • 对不起,我说的是找到两个数组的并集。您找到差异的建议很准确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
相关资源
最近更新 更多