This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Removing Integer values from ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing Integer values from ArrayList" Watch "Removing Integer values from ArrayList" New topic

Removing Integer values from ArrayList

krish rama

Joined: Feb 11, 2013
Posts: 2

I have an ArrayList which consists of Integers and String values. My requirement is how to remove integer values from arraylist. Can anyone please help me out in this regard, please?

Code snippet is something like

How to remove 10 and 2 in the above code snippet??

Thanks for help in advance
ravikumar latha

Joined: Feb 11, 2013
Posts: 1
Hi Krishna,

You can remove the integers from arraylist by using remove method of iterator interface.

Here is the solution for your issue.

Hope this solution can be helpful for your requirement
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43935
Welcome to the Ranch both of you.

I shall try to use the code button on your posts; it makes the code look a lot better.
That method of removal would work nicely, but this is begging the question:
Why have a List containing mixed Integers and Strings at all?
Please explain that, and we shall see if we can’t produce a more object‑oriented alternative.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
While it's a good question why there are both Strings and Integers in this list, the Strings don't really seem to have anything to do with the actual question or its answer. If we simply ignore the strings, the whole thing seems to make more sense.

ravikumar latha wrote:You can remove the integers from arraylist by using remove method of iterator interface.

Or more conveniently in this case, there's a remove method on the ArrayList itself.

The problem here is that there are two remove() methods on List - a remove(Object), and a remove(int). If you use the remove(int), the int argument is taken to be the index. If you use remove(Object) it's taken to be the value. From the way krish describes the problem, we probably want to remove the value 10, not the 10th element. So we need to make sure that we use the Object version of remove():
I agree. Here's the link:
subject: Removing Integer values from ArrayList
jQuery in Action, 3rd edition