39

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?

sam1370
  • 335
  • 2
  • 18
  • 11
    Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. –  Jul 27 '12 at 09: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 – Richard Tingle Jun 29 '13 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. – Ky - Mar 02 '15 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. – ʇolɐǝz ǝɥʇ qoq Mar 16 '15 at 00: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 – sam1370 Jul 02 '19 at 22:10

3 Answers3

49

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

Pshemo
  • 122,468
  • 25
  • 185
  • 269
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);
}
ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
Achilles
  • 1,065
  • 2
  • 13
  • 29
  • 2
    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 – Ky - Mar 02 '15 at 15:20
  • 1
    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 – Zoe Aug 31 '17 at 09:52
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)); 
Dharman
  • 30,962
  • 25
  • 85
  • 135
Tawfik Yasser
  • 86
  • 2
  • 5