Java 整数集交集
下面是一个创建整数集的程序,我相信我的一切都正常工作了,除了intersectionWith函数没有正常工作 这是我的IntSet代码:Java 整数集交集,java,arrays,object,set,Java,Arrays,Object,Set,下面是一个创建整数集的程序,我相信我的一切都正常工作了,除了intersectionWith函数没有正常工作 这是我的IntSet代码: public class IntSet{ private final int MAXALLOWEDSETVALUE=2000; private boolean [] data = new boolean[MAXALLOWEDSETVALUE+1]; public IntSet(int... elts) { for(in
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;
}
}
除了我与功能的交叉外,一切似乎都在为我工作
这是我运行代码时输出的内容:
is3 (intersection): {2, 4, 5, }
但它需要的只是{2,5}
我不确定我是如何得到这个错误的
我的intersectionWith函数应该基于两个集合创建一个新集合。只有在两个集合中都存在元素时,才会将该元素添加到新集合中。对不起,伙计们,看起来我已经解决了这个问题。我只需要添加一个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;
}
我将仅发布相关代码,并在添加更改的地方添加注释:
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;
}
}
这是太多的代码,无法期望人们为您处理。你需要调试来缩小问题的范围。很抱歉,我实际上应该删除大部分代码,因为它是不相关的。我不知道问题是什么,但有2条注释:1。什么是newSectionSet.toString;该怎么办?您创建了该集合的字符串版本,然后将其丢弃。2.你几乎不应该写==真;它在99.9%的情况下是多余的。if的目的是检查其条件是否正确。根据上述代码,is1没有任何错误elements@Coder117这是一个选择,但也有更干净的方法。谢谢,但我已经找出了问题所在,并发布了我自己的答案。不过我要修改我原来的问题,你能看一下吗?非常感谢你!当然可以事实上,您发现了这个bug:因为您使用了一个副本,但只修改了副本和其他副本都为真的地方——只有副本为真,而其他副本为假的地方,没有修改。这就是为什么我写道,一个明确的方法是不使用副本并修改它,而是创建一个新的空整数集并使用它谢谢!我更喜欢你的方式。
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;
}
}