Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: strange for-loop what is it doing?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default strange for-loop what is it doing?

    hey,

    So I'm learning java right now, and I saw something strange in a programming tutorial on Youtube.

    this is the normal for loop:

    // example
    for (int x = 0 : x <= 5 : x ++) {
    //code here
    }

    but then I saw something like this:

    for ( String s : getConfig().getConfigurationSection("kits"))
    {
    // code here
    }


    only 2 things inside the "()" things (don't know the english word for that ..)
    I mean not :

    for ( thing 1 : thing 2 : thing 3)

    but

    for (thing 1 : thing 2)


    what does that do ?
    I mean is that an infinite loop or what .. I don't understand that ..

    thanks for help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: strange for-loop what is it doing?

    Take a look at the tutorial-
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    BTW The clause separators in the first example should be ; not :
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    oblacker (February 12th, 2013)

  4. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: strange for-loop what is it doing?

    Quote Originally Posted by Norm View Post
    Take a look at the tutorial-
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    BTW The clause separators in the first example should be ; not :
    yea I know .. stupid me XD

    thanks

  5. #4
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default Re: strange for-loop what is it doing?

    .

  6. #5
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: strange for-loop what is it doing?

    Not just an array, but any Collection or array.

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: strange for-loop what is it doing?

    Quote Originally Posted by aesguitar View Post
    Not just an array, but any Collection or array.
    Not just an array or Collection, but any class, even a class that is not a collection, that implements the Iterable interface can be iterated via a foreach loop.

  8. #7
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: strange for-loop what is it doing?

    for (thing 1 : thing 2)
    This for loop work like below
    String[] alphabet={"a","b","c"};
    for (String str: alphabet) {
    System.out.println("Alphabet is: " + str);
    }
    The variable str holds the current value from the alphabet array

    The result of the program is:
    Alphabet is: a
    Alphabet is: b
    Alphabet is: c
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

Similar Threads

  1. Please help this is strange.
    By MillerJLee79 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2012, 04:01 AM
  2. Strange boxes appearing
    By fishnj1333 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2012, 05:36 AM
  3. something strange
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 08:58 PM
  4. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  5. Strange Compiling Error
    By crism85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2009, 12:59 AM