1

In a task I have I need to union two sets(arrays of integers), but from some reason i'm getting the wrong output...

this is the attributes of the class set:

private int []set; 
private int counter;
private int max = 10;

this is the constructor I use to get numbers(i needed to use varags because i need to be able to change the number of elements in the set):

public Set (int...numbers) {

        set = new int[max];
        counter = 0;

        for (int i = 0; i < numbers.length; i++) {

            if (!this.isMember(numbers[i])) { 
                this.set[this.counter] = numbers[i];
                this.counter++;
            }
        }
    }

this is the union method:

public Set union (Set setToUnion) {

    Set setToReturn;

    if (this.equals(setToUnion)){
        setToReturn = new Set(setToUnion);
        return setToReturn;
    }

    setToReturn = new Set(this.set);

    for (int i = 0; i < setToUnion.counter; i++) {

        if (!setToReturn.isMember(setToUnion.set[i])) {
            setToReturn.set[setToReturn.counter ] = setToUnion.set[i];
            setToReturn.counter++;
        }
    }
    return setToReturn;
}

and if you want to see how the isMember looks so here it is(just checking if some number is a member of other set):

public boolean isMember (int n) {

        if (isEmpty()) {
            return false;
        }

        for (int i = 0; i < this.counter; i++) {

            if (n == this.set[i]) {
                return true;
            }
        }

        return false;
    }

Now in the program I created two sets like this:

Set someSet = new Set(1,2,3,4,5);       
Set anotherSet = new Set(2,6,7,8,9);

Set m = someSet.union(anotherSet);

m.show();

but the output I'm getting is (1,2,3,4,5,0,6,7,8,9) and i need the same one but without the 0 in the middle...

please help thanks!

6
  • 3
    Where's the union method?
    – ooga
    Jan 22, 2014 at 19:04
  • sorry just chenched it :/ my problem is with the union @ooga
    – JohnBigs
    Jan 22, 2014 at 19:09
  • So the question title is wrong? Jan 22, 2014 at 19:10
  • it was, i changed it too, sorry for that guys @DavidWallace
    – JohnBigs
    Jan 22, 2014 at 19:11
  • Are you sure you're not missing some {} characters in your code? In particular, the ones around the second if block inside union? Jan 22, 2014 at 19:16

1 Answer 1

5

The problem appears to be this line in the union method:

setToReturn = new Set(this.set);

It should probably be:

setToReturn = new Set(this);

Why is that causing a problem? At this point, this.set equals [1, 2, 3, 4, 5, 0, 0, 0, 0, 0], which is an int array. Therefore, the following constructor will be called:

public Set (int...numbers) { ... }

That means all those numbers in the array will be added, including the zeroes. Either you need to truncate the zeroes from the array by doing something like...

setToReturn = new Set( Arrays.copyOf( set, counter ) );

or have a constructor that accepts a Set. That way you can access counter to see how many numbers in the array are actually part of the Set. I guessed you already have that constructor, based on another part of the code that calls such a constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.