aspose file tools*
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
Author

Removing Integer values from ArrayList

krish rama
Greenhorn

Joined: Feb 11, 2013
Posts: 2
Hello,

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
Regards,
Krishna
ravikumar latha
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
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: 3007
    
    9
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: http://aspose.com/file-tools
 
subject: Removing Integer values from ArrayList