• 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

for-each loop vs Iteraor

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am solving some exercises in generics and I came to this problem. the book states that for-each loops can not change  a Collection object instead I need an Iterator.
so  please help me understand what is the deference between these tow methods , I find them Identical !

my metod :=



book method :

 
Saloon Keeper
Posts: 15559
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to run your method on an ArrayList containing at least one item that matches the predicate.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Stephan !

I have tried this code below as you asked but got a strange error!




sammy
-----------------------------------------
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at chapter10.Ex4$Predicates.remove(Ex4.java:46)
at chapter10.Ex4.main(Ex4.java:33)
C:\Users\Sami Kassoum\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

 
Stephan van Hulst
Saloon Keeper
Posts: 15559
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, that's expected behavior, and that's why you can't use remove() in an enhanced for-loop.

The reason is because after you remove an element, the iteration may continue in an unexpected manner. Here is an example:

Should the result be:or:
When you use Iterator.remove() instead, this is not a problem because the iterator itself knows what the next element should be.

Note that you can make your code much nicer if you use generics and lambdas properly:

You should also use type bounds on your method parameters:
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:


Psst: Collection.removeIf(Predicate)
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Note that you can make your code much nicer if you use generics and lambdas properly:


You can use a method reference there:
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic