【问题标题】:Is there something wrong with the code I made?我做的代码有问题吗?
【发布时间】:2020-02-03 19:33:30
【问题描述】:

所以我们的项目都是关于集合的,它应该显示并集、交集、差异,我对我的代码有疑问,因为在我们老师给我们的示例中,集合的元素已经给出并且在输出在并集和交集中没有“null”结果但我们的挑战是让元素成为用户输入并且在我的代码中我的并集和交集中有“null”结果。可以吗?

  public static void main(String[] args) {


    Scanner scan = new Scanner(System.in);
    Set<Integer> a = new HashSet<Integer>();
    a.addAll(Arrays.asList(new Integer[5]));
    for () {
       //scan code...
    }





    Set<Integer> b = new HashSet<Integer>();
    b.addAll(Arrays.asList(new Integer[5]));
    for () {
       //scan code...
    }

    // UNION

    Set<Integer> union = new HashSet<Integer>(a);
    union.addAll(b);
    System.out.print("\nUnion of the two Set: ");
    System.out.println(union);


    // INTERSECTION

    Set<Integer> intersection = new HashSet<Integer>(a);
    intersection.retainAll(b);
    System.out.print("Intersection of the two Set: ");
    System.out.println(intersection);


    // DIFFERENCE

    Set<Integer> difference = new HashSet<Integer>(a);
    difference.removeAll(b);
    System.out.print("Difference of the two Set: ");
    System.out.println(difference);

}

输出:(教师给定的代码!)

两个Set[0, 1, 2, 3, 4, 5, 7, 8, 9]的并集

两个Set[0, 1, 3, 4]的交集

两个Set[2, 8, 9]的区别

我的输出:

A组:

3 4 2 1 0

B组:

7 4 1 9 8

两个集合的并集:[null, 0, 1, 2, 3, 4, 7, 8, 9]

两个Set的交集:[null, 1, 4]

两个Set的差:[0,2,3]

【问题讨论】:

    标签: java set-intersection set-difference


    【解决方案1】:

    由于代码中的这两行,您有 null :

            a.addAll(Arrays.asList(new Integer[5]));
            b.addAll(Arrays.asList(new Integer[5]));
    

    只需删除这两行,您的代码就可以工作了。

    【讨论】:

    • @Hensely 这可能是您的解决方案。但请确保在继续之前问自己这些问题(并为它们找到满意的答案):1)这些线路是如何导致问题的? 2)您希望/期望与他们一起完成什么? 3) 你为什么认为它们是必要的(为什么它们实际上不是必要的)?
    【解决方案2】:

    我同意@theincrediblethor,但作为解释......

    您正在初始化一个 Set,就好像它是一个数组一样。最初 Set 为空,并且数组未初始化。您不想将“空”整数 [null, null, null, null, null] 放入 Set 中,即使您在输入中添加真正的整数,它们也会保留在那里。

    由于 Set 不允许重复,因此在添加时删除了 4 个空整数。

    所以你只剩下 1 个 null 和你所有的输入。

    【讨论】:

      【解决方案3】:

      在你的行中

      a.addAll(Arrays.asList(new Integer[5]));
      

      new Integer[5] 正在创建一个包含 5 个空值的数组 [ null,null,null,null,null ]

      Arrays.asList() 只是把它变成一个包含 5 个空值的 Collection(List)

      a.addAll() 将 Collection 的所有元素添加到 Set 中,并且由于值是唯一的,因此您只需添加一个 null

      所以你只需要删除该行,对于 Set b 也是如此

      试试这个代码,你会看到一步一步的

      import java.util.Set;
      import java.util.HashSet;
      import java.util.Arrays;
      
      public class Main
      {
          public static void main(String[] args) {
              Integer[] arr = new Integer[5];
              System.out.println(Arrays.toString(arr));
              Set<Integer> a = new HashSet<Integer>();
              a.addAll(Arrays.asList(arr));
              System.out.println(a);
              a.add(1);
              System.out.println(a);
          }
      }
      

      打印出来

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-12
        • 2011-05-31
        • 2013-05-04
        相关资源
        最近更新 更多