• 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

How to remove odd number from a arraylist by use iterator remove method?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
create a new MyLinkedList<Integer> object named myList
insert integers multiples of 3 from 6 to 45 into myList
print myList
create an iterator for myList
remove (use iterator’s remove method) all numbers multiples of 9 or even from myList
print myList
remove (use iterator’s remove method) all odd numbers from myList
print myList
------------------------------------------------
There is my code. I am not sure how to write the third part to remove all odd number from myList.


 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in the prior loop you check for even numbers...so...
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are removing element when it is even and also in case when is divided by 9
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, you could just iterate over the List and simply call remove on every entry.
 
Jessica Jones
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, after I removed the even number and multiples of 9, then I want to do remove odd number, how can I do that?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jessica Jones wrote:Yes, after I removed the even number and multiples of 9, then I want to do remove odd number, how can I do that?


You have already been given all the hints you need.

Your code currently checks for even numbers, right?  If you know how to check if a number is even, then checking if a number is NOT even is very similar.

Also, Dave pointed out that once you remove multiples of 9 and even numbers from your list, all you will be left with are odd numbers.  That means you can basically iterate over the list again and remove each remaining item without even checking them against any condition. Because of what you already did to the list previously, you are guaranteed to have only odd numbers in there, so making a check for odd number is pretty much a redundant effort.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, Welcome to the Ranch!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There must be a way to do that with a Java8 Stream:-As you know, all Lists have a stream() method, which returns (in the case of a List<Integer>) a Stream<Integer>. You can use the filter() method with a Predicate as its method argument to create a second Stream with those objects which fulfil the Predicate shown. In this case I am passing a λ which tests for odd numbers using boxing and unboxing. The javac tool will “know” that Streams contains Integers which can be boxed and unboxed.
Finally, you can collect the contents of the Stream; you need to pass a Collector reference; the eaiest way to do that in this case is to use the ready‑made Collector supplied by this Collectors class method. That will specifically create a List<Integer> in the current example.
 
Jessica Jones
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for  all of the responding .
yeah, I am new to this forums, new to java.
Thanks for helping .
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic