44

I have been asked to use the enhanced for loop in my coding.

I have only been taught how to use traditional for loops, and as such don't know about the differences between it and the enhanced for loop.

How does an enhanced for loop differ from a traditional for loop in Java?

Are there any intricacies that I should look out for which tutorials tend not to mention?

5
  • 12
    Welcome to Stack Overflow! We encourage you to research your questions. If you've tried something already, please add it to the question - if not, research and attempt your question first, and then come back. Commented Jul 27, 2012 at 9:46
  • 8
    Although, if user1920811 had researched it and had found the answer really easily online and so hadn't asked this question I wouldn't have been able to find the exact syntax I required as the second link on google Commented Jun 29, 2013 at 15:49
  • 12 people upvoted this, and 21 its answer. Perhaps this should be reopened? I figured that questions that so completely go against the site that they are voted closed would not have such a positive community response. Commented Mar 2, 2015 at 15:24
  • @BenC.R.Leggiero 14 people upvoted this, and 22 its answer. Perhaps this should be reopened? I figured that questions that so completely go against the site that they are voted closed would not have such a positive community response. Commented Mar 16, 2015 at 0:26
  • Yes, please re-open this, user1920811 just asked about the syntax. Often I find stackoverflow questions contain more information than the tutorials posted online Commented Jul 2, 2019 at 22:10

4 Answers 4

55

Enhanced for loop:

for (String element : array) {

    // rest of code handling current element
}

Traditional for loop equivalent:

for (int i=0; i < array.length; i++) {
    String element = array[i]; 

    // rest of code handling current element
}

Take a look at these forums: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html

Sign up to request clarification or add additional context in comments.

1 Comment

But...links are bad, pa! Best get to the gallows for'ta meet yer maker.
10

An enhanced for loop is just limiting the number of parameters inside the parenthesis.

for (int i = 0; i < myArray.length; i++) {
    System.out.println(myArray[i]);
}

Can be written as:

for (int myValue : myArray) {
    System.out.println(myValue);
}

2 Comments

A note: traditional for loops can also do more. Enhanced ones are solely for iterating through an array or a class that implements Iterable. Traditional for loops can also be used to loop exactly n times (for(int i=0; i<n; i++)), loop infinitely (for(;;)), to abuse capabilities (for(String name="me"; conn.isOpen(); System.out.println("hello!"))), and many other such things.
The enhanced for-lops are only good for iteration. If you need the index, or want to do hacks and/or abuse capabilities, the traditional for-loops is the way to go
0
  1. Enhanced For Loop (Java)
for (Object obj : list);
  1. Enhanced For Each in arraylist (Java)
ArrayList<Integer> list = new ArrayList<Integer>(); 
list.forEach((n) -> System.out.println(n)); 

Comments

0

I often stumble here when trying to remember the exact syntax, so I'll provide a more technical answer. Enhanced for statements are explained here in the Java Language Standard (JLS). If you want the most technical version go check that.

Essentially, an enhanced for statement can take either an array or Iterable.

Iterables

This

for (T element : iterable) {
    ...
}

Is translated into code something like this

for (Iterator i = iterable.iterator(); i.hasNext(); ) {
    T element = i.next();
    ...
}

Arrays

This

for (T element : array) {
    ...
}

Is translated into code something like this

for (int i = 0; i < array.length; i++) {
    T element = array[i];
    ...
}

Comments