public class Program {
IntegSet i1, i2, i3, i4;
i1 = new IntegSet();
i2 = new IntegSet(1,2,5);
i3 = new IntegSet();
i4 = new IntegSet(i2);
}
My program is about making integer sets.
public class IntegSet{
private final int MAXALLOWEDSETVALUE=2000;
private boolean [] data = new boolean[MAXALLOWEDSETVALUE+1];
I have this first function, and I think it's ok.
public IntegSet(int... elts) {
int index = 0;
for(int iteration = 0; iteration < elts.length; iteration++) {
index = elts[iteration];
data[index] = true;
}
}
But what about this function
public IntegSet(IntegSet source){
this.data = source.data;
}
Is this a copy constructor? I'm a little confused on how that works. And how it differs from this function:
public void setTo(IntegSet source){}
where I am supposed to call it with this:
i3.setTo(i3.subtract(i1))
Thank you
datacan be modified, you'll needArrays.copyOf(data, data.length)instead asdatais simply a reference and if the originalIntSetchanges, the new one will as well. Also, I think your original constructor is wrong. If I pass{1, 3, 5}to your constructor, yourIntSetwill set ints{0, 1, 2}instead, because you loop from0toelts.length.this.data = source.data.clone().