File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing Integer values from ArrayList

 
krish rama
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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():
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic