What is an Enhanced Loop in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is an Enhanced Loop in Java?

8th Jun 2017, 1:53 AM
Chaman Raghav
Chaman Raghav - avatar
2 Answers
+ 3
String[] myList = {Element1,Element2,Element3,Element4}; for (String element : myList) { System.out.println(element); }
8th Jun 2017, 2:41 AM
Soussi Mohamed
Soussi Mohamed - avatar
+ 2
It's for (DataType varName : list) Say it like 'for each DataType in list'. It's basically the same as writing this: for(int i = 0; i < list.length; i++) DataType varName = list[i]; Except using the Iterator class it pulls out each element one by one for you, of the type you specify. Performance wise i'm pretty sure it does better when casting is needed. Otherwise it's not much different, if not worse than the standard loop.
8th Jun 2017, 3:26 AM
Rrestoring faith
Rrestoring faith - avatar