• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enhanced loop  RSS feed

 
Saman Ghamghami
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class named "apple" and I have the following "Enhanced Loop". As far as I know you should specify the data type before the : then array name, but here the name of the class is used. What does this loop do?

Is there any other feature of the loop that I'm not aware of?

    for (apple sam : olives) {
        // do something
    }
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

but here the name of the class is used

  • As you haven't posted complete code, I assume olives is an array of type apple ( class ) where each element in array olives is an object of type apple so when for loop executes It returns an obejct of type apple which is assigned to reference variable sam of type apple here

  • Here user defined data type is apple and olives is the array name whose elements are of type apple.
  • Let's see an example of array of int and array of type Apple using enhanced for loop.

  • Hope you got that
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And Output is:
    1
    2
    3
    4
    5
    Apple Type: AppleA
    Apple Rate/Kg : 300.5
    Apple Type: AppleB
    Apple Rate/Kg : 200.5
    Apple Type: AppleC
    Apple Rate/Kg : 400.5
     
    Fred Kleinschmidt
    Bartender
    Posts: 560
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    What does this loop do?

    This is equivalent to the traditional loop:

    Is there any other feature of the loop that I'm not aware of?

    There is an additional feature that if you attempt to add to, modify, or delete an item of "olives" inside the loop you are likely to encounter a ConcurrentModificationException.

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7817
    142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred, that's not completely correct. For Iterable types, the enhanced for loop will use the iterator, never the get() method (which is not declared on Iterable in the first place). If 'olives' is an Iterable rather than an array, the loop would be equivalent to the following:

    iterator.next() is what throws the ConcurrentModificationException if the underlying data source is structurally modified in between iterations.
     
    Fred Kleinschmidt
    Bartender
    Posts: 560
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Right you are, Stephan.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is how I got both method's internal implementation after decompiling using cavaj Java Decompiler 1.11


     
    Campbell Ritchie
    Marshal
    Posts: 55761
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am relieved you didn't admit to writing that code yourself. Putting [] after the identifier. That = sign inside (). At least you can blame it all on jad
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I am relieved you didn't admit to writing that code yourself. Putting [] after the identifier. That = sign inside (). At least you can blame it all on jad
    hahaha yes behind the scene cavaj uses jad.
  • One question arises when I saw this, reducing programmer's  work of writing code was the only purpose behind enhanced for statement over traditional one?
  • Difference is, If we use enhanced for statement with array as written in my example then we can't get particular element at particular index as we can in traditional for statement.
  • When using enhanced for statement with
    • int[] It uses traditional for statement in the backgrund then what is advantage? only reducing programmer's work.
    • Collection, lets say List then It uses Iterator in traditional for statement. Advantage ? only reducing programmer's work.
    • Example of List with enhanced for statement
    • Which is implemented in background like below, after decompiled using jad command.

     
    Campbell Ritchie
    Marshal
    Posts: 55761
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:. . . One question arises when I saw this, reducing programmer's  work of writing code was the only purpose behind enhanced for statement over traditional one? . . .
    Or to catch up with languages like C# and Pascal which have had for‑each loops for up to 50 years?
    The idea of the for‑each loop i to have a simple idiom for iterating the whole array in a read‑only fashion. You can access and manipulate all elements in the array and not assign to them. I have just written an example here. Note in that example you cannot write b = something; (well actually you can, but it will have no effect). You can also use for‑each loops for Lists etc.
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Or to catch up with languages like C# and Pascal which have had for‑each loops for up to 50 years?
    If that's the one of the reasons then Isn't It too late Yes sometime we just need to iterate the whole array in a read‑only fashion in that's case It's good else performance wise I think both for statements barely differ.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!