Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

remove list item

My list contains "4" ,"5"  . How do I remove these two items from the list ?   I want to remove these values. I dont know the index .
Avatar of colr__
colr__

If it is a Collection you can just call .remove("5") on your list. This will work since Strings are immutable in Java.
list.removeAll();
All the above are noteworthy answers. You may also consider this one:

list.removeAll(Arrays.asList(new String[] { "4", "5" }));
In the ArrayList you have clear() method is there..
removeAll() and clear() are not correct, they would empty the list completly, which is not what the asker wanted - he mentioned removing 'these two items' and not knowing the index, which he WOULD if it were to remove all - the indexes would be 0 and 1!

ioanton - this is really poor practice - creating an array and an object (List), copying all the values, then iterating over the lists??!!? While this solution is correct, it is very cumbersome and computationally expensive
colr__

Thanks for your comments.  At the end of your criticism, I would have expected to see your solution.
My solution was posted in the first response to the question, however I shall repeat it for you:

list.remove("5")
Oh, sorry. It's really amazing. If it would be possible, I would remove my first poor comment. Shame on me.
Ok, you're right, I apologise.
Avatar of cofactor

ASKER

This does not work.

Here is what I tried ,

List<String> boxes=cvo.getStructureBoxes();
boxes.remove("3");
boxes.remove("7");


This has changed cvo.getStructureBoxes() List also !  ...I dont want to change cvo.getStructureBoxes()

I want to keep cvo.getStructureBoxes() list as it is ..however, I want to get a new list values after removing those items.
ASKER CERTIFIED SOLUTION
Avatar of colr__
colr__

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or, in shorter form you can also use the ArrayList constructor that takes another collection as an argument:

List<String> boxes = new ArrayList<String>(cvo.getStructureBoxes());

If you have control of the cvo.getStructureBoxes() method, you can change it to always return a copy of its List so that only it can modify its internal data.

public class CvoClass {
    private List<String> structureBoxes; // initialized somewhere

    public List<String> getStructureBoxes() {
        return new ArrayList<String>(structureBoxes);
    }
}

Open in new window


This technique is called defensive copying.
One caveat to the List.remove() method.  There are two versions of that method, one takes an Object as a parameter and one takes an int.  The remove(Object) method uses .equals() to find and remove the first element in the List that equals the provided Object.  The remove(int) method removes the element at the provided index.  This can get you into trouble if you have a List<Integer> because of the JDK 1.5+ auto-boxing.  If you have: { 4, 5, 6, 0, 1, 2 } and call remove(5), do you want to remove the element 5 (index 1) or the element at index 5 (2)?  Just an FYI and not completely applicable to your question unless you change the type of your List.