【问题标题】:Integer Sets Intersection整数集交集
【发布时间】:2017-03-10 00:57:24
【问题描述】:

以下是创建整数集的程序,我相信我一切正常,除了我的 intersectionWith 函数不能正常工作。

这是我的 IntSet 代码:

public class IntSet{
    private final int MAXALLOWEDSETVALUE=2000;
    private boolean [] data = new boolean[MAXALLOWEDSETVALUE+1];

    public IntSet(int... elts) {
        for(int iteration = 0; iteration < elts.length; iteration++) {
            if(elts[iteration] <= MAXALLOWEDSETVALUE)
            data[elts[iteration]] = true;
        }
    }

    public IntSet(IntSet source){
        System.arraycopy(source.data, 0, this.data, 0, MAXALLOWEDSETVALUE);
    }
    public void setTo(IntSet source){
        System.arraycopy(source.data, 0, this.data, 0, MAXALLOWEDSETVALUE);
    }        

    public IntSet intersectionWith(IntSet other) {
        IntSet newSectionSet = new IntSet(this);
        for(int iteration = 0; iteration < MAXALLOWEDSETVALUE; iteration++) {
            if(newSectionSet.data[iteration] == true && other.data[iteration] == true) {
                newSectionSet.data[iteration] = true;
            }
        }
        return newSectionSet;
    }
}

还有我的程序代码:

import java.lang.Math;
import java.util.Random;
public class Program {

    public static void main(String [] args){
        Random rng = new Random();
        rng.setSeed(0);
        IntSet is1, is2, is3, is4;
        is1 = new IntSet(2,4,5);
        is2 = new IntSet(1,2,5);
        is3 = new IntSet();
        is4 = new IntSet(is2);

        is3.setTo(is1.intersectionWith(is2));
        System.out.print("is3 (intersection): ");
        System.out.println(is3.toString()); // should be 2 5
    }
}

一切似乎都对我有用,除了我的 intersectionWith 函数。

这是我运行代码时输出的内容:

is3 (intersection): {2, 4, 5, }

但它需要的只是 {2, 5}

我不确定我是如何得到这个错误的。

我的 intersectionWith 函数应该基于两个集合创建一个新集合。只有当两个集合中都存在该元素时,它才会将一个元素添加到新集合中。

【问题讨论】:

  • 这是太多的代码,无法期望人们为您完成。您需要进行调试以缩小问题范围。
  • 很抱歉,我实际上应该删除大部分代码,因为它们无关紧要
  • 我不知道问题是什么,但是 2 cmets: 1. newSectionSet.toString(); 应该做什么?您制作了该集合的字符串版本,然后将其丢弃。 2. 你几乎不应该写== true;在 99.9% 的情况下它是多余的。 if 的目的是检查它的条件是否为真。
  • 根据上面的代码is1没有任何元素

标签: java arrays object set


【解决方案1】:

对不起,伙计们,看来我想通了。我只需要添加一个 else 语句:

 public IntSet intersectionWith(IntSet other) {
        IntSet newSectionSet = new IntSet(this);
        for(int iteration = 0; iteration < MAXALLOWEDSETVALUE; iteration++) {
            if(newSectionSet.data[iteration] == true && other.data[iteration] == true) {
                newSectionSet.data[iteration] = true;
            } else {
               newSectionSet.data[iteration] = false; // added this
            }
        }
        return newSectionSet;
    }

【讨论】:

  • 这是一个选项,但有更简洁的方法。
【解决方案2】:

我将只发布相关代码并在我添加更改的地方添加 cmets:

class IntSet{

    private final int MAXALLOWEDSETVALUE = 5; // modified it to a smaller number - easier to debug
    private boolean [] data = new boolean[MAXALLOWEDSETVALUE+1];

    public static void main(String [] args){
        Random rng = new Random();
        rng.setSeed(0);
        IntSet is1, is2, is3;
        is1 = new IntSet(1,2,3);
        is2 = new IntSet(1,2,5);
        is3 = is1.intersectionWith(is2); // I modified the test cases
        System.out.println(is1); // [false, true, true, true, false, false, false]
        System.out.println(is2); // [false, true, true, false, false, true, false]
        System.out.println(is3); // [false, true, true, false, false, false, false]
    }

    @Override
    public String toString() { // added a toString method for a nicer printing
        return Arrays.toString(data);
    }

    public IntSet(int... elts) {
        for(int iteration = 0; iteration < elts.length; iteration++) {
            if(elts[iteration] <= MAXALLOWEDSETVALUE)
                data[elts[iteration]] = true;
        }
    }

    public IntSet intersectionWith(IntSet other) {
        IntSet newSectionSet = new IntSet(); // instead of copying and modifying the copy - just create an "empty" set and update it
        for(int iteration = 0; iteration < MAXALLOWEDSETVALUE; iteration++) {
            if(data[iteration] && other.data[iteration]) { // now the comparison is done directly against `data`
                newSectionSet.data[iteration] = true;
            }
        }
        return newSectionSet;
    }
}

【讨论】:

  • 谢谢,但我已经弄清楚出了什么问题,我发布了自己的答案。但是我要编辑我原来的问题,你认为你可以看一下吗?非常感谢!
  • 没问题!确实你发现了这个错误:因为你使用了“副本”但只修改了副本和“其他”都有true的地方 - 只有副本有true但其他有false的地方没有修改.这就是为什么我写了一个明确的方法是使用副本并修改它,而是创建一个新的空 IntSet 并使用它
  • 谢谢!我更喜欢你的方式。
猜你喜欢
  • 2017-09-04
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多