0

I have seen this kind of for-loop around but I don't quite understand it. Can someone explain how it works?

Example from the link:

public static void printPersonsWithinAgeRange(
    List<Person> roster, int low, int high) {
    for (Person p : roster) {
        if (low <= p.getAge() && p.getAge() < high) {
            p.printPerson();
        }
    }
}

EDIT: question already answered here: How does the Java 'for each' loop work?.

EDIT 2: Oracle tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Community
  • 1
  • 1
uranibaba
  • 712
  • 1
  • 13
  • 30
  • it is a shortcut for iterator. It will give you a referennce to the next element in the list at every loop. – Jens May 15 '15 at 11:36
  • 1
    Possible duplicate of http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work – justAbit May 15 '15 at 11:38
  • 1
    It is known as the [*Enhanced for-loop*](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). Oracle recommends *"using this form of the for statement instead of the general form whenever possible."* – Mr. Polywhirl May 15 '15 at 11:47
  • Thanks , @Mr.Polywhirl This is what I was looking for, adding to original post. – uranibaba May 15 '15 at 11:53

0 Answers0