1

I am trying to compare weather an item is contained within the arrayList and if it is then remove it. Im not sure weather I have to implement my own equals method or not, this is my code but it isn't working to remove the correct item.

  public boolean removeItem(Item item) {

    for(int i = 0; i < items.size(); i++) {

        if (items.get(i).equals(item)) {
            items.remove(item);
            return true;
        } 


    }
    return false;
}
6
  • 1
    Check out the contains and remove methods of ArrayList. Commented Oct 31, 2013 at 13:51
  • 3
    And better use iterator and remove() on iterator to avoid concurrent modification exception. Commented Oct 31, 2013 at 13:52
  • 1
    yes, you have to implement equals and hashCode for your Item class Commented Oct 31, 2013 at 13:52
  • I think you want contains not equals`. You don't even need to loop it. Commented Oct 31, 2013 at 13:54
  • @Nambari, I don't think such an exception would be possible here as Luke14 is using an "old-school" for loop instead of for each. A good principle, though! Commented Oct 31, 2013 at 13:56

4 Answers 4

2

ArrayList#remove(Object) will do exactly that! But this works only if you have overridden the equals method of Item. If You want to remove all elements, will need a loop:

public int remove(Item item) {
    int i = 0;

    while(list.remove(item)) {
        i++;
    }

    return i;
}

this will return the amount of items that have been removed

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this loop will look for the elements to remove from the start after every item removed. It has a complexity O(k*n) where k is list.size() and n is the number item currencies. This could be done i O(k) using list.iterator() and iterator.remove().
2

You can safely remove items from a Collection using Iterator

public boolean removeItem(Item item) {    
  Iterator<Item> it = items.iterator();
  while (it.hasNext()) {
     Item i = it.next();
     if(i.equals(item)) {
       it.remove();
       // remove next line if you want to remove all occurrences `item`
       return true; 
     }      
  }
  return false;
}

You could also just call

items.remove(item);

Comments

0

I think you want contains not equals`. You don't even need to loop it. That's the magic of the method.

public boolean removeItem(Item item) {

    if (items.contains(item)){
        items.remove(item);
    }
    return false;  // I have no idea why you want to return false.
                   // I'll just leave it there
}

3 Comments

this will always return false! romove is no function of arraylist
you have a typo on remove
This traverses the list twice--once for contains() and again for remove(). You might as well just call remove() and let it fail if the item isn't in the list.
0

Yes you have to override equals in your class.Compare every single attribute and when one of them dont equals the other, then return false. If you use eclipse for example, you can let it create the equals method for your class.

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.