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 .
cofactorAsked:
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.

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

list.removeAll(Arrays.asList(new String[] { "4", "5" }));
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

chaitu chaituCommented:
In the ArrayList you have clear() method is there..
0
colr__Commented:
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
0
ioantonCommented:
colr__

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

list.remove("5")
0
ioantonCommented:
Oh, sorry. It's really amazing. If it would be possible, I would remove my first poor comment. Shame on me.
0
colr__Commented:
Ok, you're right, I apologise.
0
cofactorAuthor Commented:
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.
0
colr__Commented:
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();
boxes.addAll(cvo.getStructureBoxes());

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

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
gordon_vt02Commented:
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.
0
gordon_vt02Commented:
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.
0
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
Java

From novice to tech pro — start learning today.