Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By Norm
  • 1 Post By jim829
  • 1 Post By jim829

Thread: What does this snippet do?

  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default What does this snippet do?

    Would you please describe what this code does?

    for (int i:a) { f(i); }

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: What does this snippet do?

    Read about enhanced for statement at the bottom of this page: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    You need to post the definition for the variable: a
    ghandil likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What does this snippet do?

    What do you think it does? Look at each part of the code and describe what that part is doing. For example, do you know what
    this does?

    Java Code:
    for (int i: a)
    Do you know what this does?

    Java Code:
    f(i);
    In some cases, the description will be rather general.

    Regards,
    Jim
    ghandil likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What does this snippet do?

    You need to post the definition for the variable: a
    Actually it's not needed. One can assume a implements Iterable<Integer>. Otherwise, the compiler would complain (assuming the snippet in isolation is
    correct of course).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: What does this snippet do?

    Quote Originally Posted by Norm View Post
    Read about enhanced for statement at the bottom of this page: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Thanks, It was clear.
    Quote Originally Posted by jim829 View Post
    What do you think it does? Look at each part of the code and describe what that part is doing.
    Thank you. The different syntax (i:a) in front of for, was vague to me. The documentation link was clarifying.
    I also should get to know the 'Iterable<Integer>' you mentioned.

    Regards

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What does this snippet do?

    I hadn't considered the for-each loop confusion. Norm's link was the key. And Iterable<Integer> is interesting but understanding the Iterator interface itself will probably be more useful.

    Regards,
    Jim
    ghandil likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Use a snippet ?
    By huuduy216 in forum New To Java
    Replies: 4
    Last Post: 09-23-2013, 07:50 PM
  2. what this snippet will do?
    By fredfunk in forum New To Java
    Replies: 4
    Last Post: 11-04-2012, 10:07 PM
  3. Code snippet pls?
    By Srinivas0615 in forum New To Java
    Replies: 1
    Last Post: 07-29-2011, 11:32 PM
  4. what are the problems of the snippet
    By QkrspCmptPop in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-26-2010, 02:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •