• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing Integer values from ArrayList

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic