3

I know my add method is correct:

public void add(Object object) {
    if (!contains(object) && size !=maxObjects) {
        set[size] = object; size++;
    }
    else 
        System.out.println("Already exists.");
}

because I get print outs like:

Set [maxObjects=8, set=[a, 7, null, null, null, null, null, null], count=2]  
true  (I ask if it contains a value)  
false  "                            "  
Set [maxObjects=8, set=[a, 7, b, Valencia, 24, s, Victoria, null], count=7]  
Set [maxObjects=8, set=[a, 7, b, Valencia, 24, s, Victoria, 4234], count=8]  

I have tried two different remove methods that are both the same (one I created; the other I found on Stack in the most similar problem a few days ago.)

1st remove:

public boolean remove(Object object) {
    if (contains(object)) {
        object = null; 
        return true;
    }
    System.out.println("The object doesn't exist to delete.");
    return false;
}

The other remove:

public boolean remove(object object) {
    for (int i=0; i<(size-1); i++) {
        while (!contains(object) && size <= maxObjects) {
            set[i] = set[i+1]; size--; return true;
        }
    }
    System.out.println("Doesn't exist.");
    return false;
}

Any help would be amazing!

4
  • 2
    I'm sorry to say, but (the unedited version of, thanks @TheStijn) your code looks awful. And shouldn't compile anyway (object). Somehow I'm reminded of this comic... Commented Oct 19, 2012 at 9:40
  • why do you call it a list when you're implementing a set? Commented Oct 19, 2012 at 9:50
  • Um, all I see are format corrections. Correct formatting is for the programmer. It's 5am and I "start" my day in two hours. My apologies. Although I laughed at the comic, I'm still offended (ha!). Commented Oct 19, 2012 at 10:04
  • I didn't mean to add confusion. This is from a Set ADT implementation, but we're studying lists. It is indeed time for bed. Commented Oct 19, 2012 at 10:06

2 Answers 2

1

You have to find the object in the array and then for example move the last object to that index (if it isn't already the last) and decrement size.

if (obj == null) return;

for (int i = 0; i < size; i++) {
    if (obj.equals(set[i])) {
        set[i] = set[--size];
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your remove method is doing nothing but checking the presence of the object in the list.

Setting null to an object does not remove it from the list. To remove the object from the list, you have to do the following:

  1. Find the position of the object in the array
  2. Remove the found position from the array.

To remove the position, you could either shift all values after the index to left (like the second algorithm try to do). Or you could create a new array and copy the content from the old to the new one (skipping the found index).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.