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 .
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

list.removeAll(Arrays.asList(new String[] { "4", "5" }));
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

chaitu chaituCommented:
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

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:

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.
cofactorAuthor Commented:
This does not work.

Here is what I tried ,

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

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.
Thats because in java the objects are passed by reference. When you call List<String> boxes=cvo.getStructureBoxes(), boxes does nto become a new instance of the list, it merely points to the list in cvo.getStructureBoxes().

What you want to do is create a new list from the items in the original:

List<String> boxes=new ArrayList<String();

... then you could call boxes.remove("4") and it would remove from boxes but leave cvo.getStructureBoxes() intact.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.